Stv*_*dll 6 spring tomcat spring-aop context.xml load-time-weaving
我使用的是Spring 3.2.9,Tomcat 6.0.44
我正在尝试配置我的应用程序的Spring检测提供程序(例如spring-instrumentation.jar)进行加载时编织,当它部署在Tomcat上时.
我要求不要在命令行上使用"-javaagent:/path/path/spring-instrument.jar"来进行配置.
我已经读过,我可以通过修改应用程序的Tomcat配置的<Context>元素来配置Spring检测(在Tomcat的server.xml或我的web应用程序的context.xml中).将适当的<Context>元素添加到server.xml会导致我的应用程序正确配置为与Spring的检测提供程序一起运行.将它添加到context.xml(见下文)不会导致工作设置.
我有一个META-INF/aop.xml文件,如下所示:
<aspectj>
<weaver options="-verbose -showWeaveInfo -debug">
<include within="com.mv.xx.services..*"/>
<exclude within="com.mv.xx.aop..*"/>
</weaver>
<aspects>
<aspect name="com.mv.xx.aop.MyAspect"/>
</aspects>
</aspectj>
Run Code Online (Sandbox Code Playgroud)
我还指定我想通过将其添加到我的Spring上下文配置来使用加载时编织:
<context:load-time-weaver />
Run Code Online (Sandbox Code Playgroud)
我将这个jar添加到我的应用程序的类路径:spring-instrument-tomcat.jar
我做了什么:
启动Tomcat时,如果我使用-javaagent参数在命令行上识别spring-instrument.jar的位置,如下所示:
-javaagent:/path/path2/spring-instrument-3.2.9.RELEASE.jar
Run Code Online (Sandbox Code Playgroud)
一切正常.
接下来,我从命令行中删除了"-javaagent:/path/path2/spring-instrument-3.2.9.RELEASE.jar".在Tomcat的server.xml文件(位于$ CATALINE_HOME/conf中)中,我向<Host>元素添加了一个<Context>元素,如下所示:
<Context path="/myApp" docBase="myApp">
<Loader loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader"/>
</Context>
Run Code Online (Sandbox Code Playgroud)
使用此配置,一切都正常.但是我要求不修改Tomcat的server.xml,因为我无法控制server.xml(DevOps会这样做,并且不愿修改它).
接下来,我从Tomcat的server.xml中删除了<Context>元素.根据Spring文档,我可以在我的webapp中添加/META-INF/context.xml,并将以前在Tomcat的server.xml中的<Context>元素放入context.xml中,如下所示:
<Context>
<Context path="/myApp" docBase="myApp">
<Loader loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader"/>
</Context>
</Context>
Run Code Online (Sandbox Code Playgroud)
但是,当我重新启动Tomcat时,我在日志中收到一条错误消息:
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.context.weaving.AspectJWeavingEnabler#0': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loadTimeWeaver': Initialization of bean failed; nested exception is 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
Run Code Online (Sandbox Code Playgroud)
在挖掘之后,我阅读了一些建议我修改<context:load-time-weaver/>Spring配置中的元素的内容,如下所示:
<context:load-time-weaver weaver-class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
Run Code Online (Sandbox Code Playgroud)
并将包含InstrumentationLoadTimeWeaver.class的jar添加到我的类路径中.
但是,当我这样做时,我在日志中收到此错误消息:
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
java.lang.IllegalStateException: Must start with Java agent to use InstrumentationLoadTimeWeaver. See Spring documentation.
etc....
Run Code Online (Sandbox Code Playgroud)
任何人都可以解释如何使用Spring和Tomcat设置加载时编织而不使用命令行上的-javaagent,并且不修改server.xml?
这是我设法使用的代码,以删除您提到的异常。基本上,您必须实现 LoadTimeWeavingConfigurer 并重写方法 getLoadTimeWeaver()。
@Configuration
@ComponentScan(basePackages = "org.myproject")
@EnableAspectJAutoProxy
@EnableSpringConfigured
@EnableLoadTimeWeaving(aspectjWeaving = EnableLoadTimeWeaving.AspectJWeaving.AUTODETECT)
public class Config implements LoadTimeWeavingConfigurer {
@Override
public LoadTimeWeaver getLoadTimeWeaver() {
return new ReflectiveLoadTimeWeaver();
}
@Bean
public InstrumentationLoadTimeWeaver loadTimeWeaver() throws Throwable {
InstrumentationLoadTimeWeaver loadTimeWeaver = new InstrumentationLoadTimeWeaver();
return loadTimeWeaver;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4472 次 |
| 最近记录: |