How to make Tomcat work with TomcatInstrumentableClassLoader defined in META-INF/context.xml together with WEB-INF/lib/spring-instrument-tomcat.jar

aka_sh picture aka_sh · Jun 28, 2012 · Viewed 17k times · Source

I'd built a simple app which uses Spring Data 3.1.0.RELEASE and Eclipselink 2.4 on the server side - which is hosted in the Tomcat 7.0.27. In mywebapp/META-INF/context.xml I have Spring classloader

<Loader loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader"/>

If I put spring-instrument-tomcat.jar into $TOMCAT_HOME/lib then Tomcat runs mywebapp well but if I put spring-instrument-tomcat.jar into the $TOMCAT_HOME/webapps/mywebapp/WEB-INF/lib - Tomcat dies with exception

java.lang.IllegalStateException: ClassLoader [org.apache.catalina.loader.WebappClassLoader] does NOT provide an 'addTransformer(ClassFileTransformer)' method. Specify a custom LoadTimeWeaver or start your Java virtual machine with Spring's agent: -javaagent:org.springframework.instrument.jar

Context loader parameter useSystemClassLoaderAsParent set to false does not help either.

I don't want to have any (custom) global lib in Tomcat (and it won't be easy to have the global one in our real-life production system) thus I'm trying to localize all the needed stuff in simple (yet big) war file. Any thoughts on how to achieve such encapsulation?

Answer

rodche picture rodche · Jan 9, 2013

Well, if you do not want extra jars in Tomcat's lib folder, the only option (to still have Spring / AspectJ LTW work) is to edit the tomcat's run script to add -javaagent:...instrument.jar to its JAVA_OPTS or CATALINA_OPTS, and remove the context.xml file (you won't need TomcatInstrumentableClassLoad‌​er anymore).