2016-03-14 19 views
6

在-javaagent下,Tomcat 6.0.44如何设置负载时间与Spring和Tomcat织,不许使用我使用Spring 3.2.9命令行

我想配置我的应用程序的Spring仪器提供商(例如spring-instrumentation.jar)用于加载时织入,当它被部署在Tomcat上时。

我有一个不使用的要求: “-javaagent:/path/path/spring-instrument.jar”在命令行上进行配置。

我读过我可以通过修改应用程序的Tomcat配置(在Tomcat的server.xml或我的web应用程序的context.xml中)的<Context>元素来配置Spring工具。将适当的<上下文>元素添加到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> 

我还规定,我想加入这个我Spring上下文配置使用负载时编织:

<context:load-time-weaver /> 

我补充这个坛子我的应用程序的类路径: 弹簧仪器tomcat.jar

我曾尝试:

当启动Tomcat,如果我确定使用-javaagent参数这样的命令行上的弹簧instrument.jar的位置:

-javaagent:/path/path2/spring-instrument-3.2.9.RELEASE.jar 

,一切工作正常。在命令行:


接着我删除 “/path/path2/spring-instrument-3.2.9.RELEASE.jar -javaagent”。 在Tomcat的server.xml文件(位于$ CATALINE_HOME/conf目录),我加了一个<上下文>元素的<主机>元素,像这样:

<Context path="/myApp" docBase="myApp"> 
    <Loader loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader"/> 
</Context> 

利用这种配置的一切行为正确。不过,我有一个要求,不要修改Tomcat的server.xml,因为我无法控制server.xml(DevOps所做的并且不愿修改它)。


接下来我删除从Tomcat的server.xml中<语境>元素。 根据Spring docs,我可以将/META-INF/context.xml添加到我的webapp,并将原来在Tomcat的server.xml中的<Context>元素放入上下文中。XML,像这样:

 <Context> 
      <Context path="/myApp" docBase="myApp"> 
       <Loader loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader"/> 
      </Context> 
     </Context> 

然而,当我重新启动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 

周围挖掘后,我读了一些建议我修改<context:load-time-weaver/>元素在我的Spring配置中,像这样:

 <context:load-time-weaver weaver-class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" /> 

并将包含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.... 

谁能解释如何设置加载时间与Spring和Tomcat织不使用命令行-javaagent,并没有修改server.xml?

+0

同样的问题!任何解决方案 – HVT7

回答

0

这是我设法用来删除您提到的异常的代码。 基本上你必须实现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; 
    } 

} 
+0

这可以从基于xml的配置完成吗? – Ryan

+0

@Ryan我可能会在xml配置中实现相同的功能。看看春季文档 - http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/EnableLoadTimeWeaving.html这可能有所帮助。谢谢 – Gui

+0

感谢您的链接。这看起来比我发现的更详细。它没有在我的第一次尝试,但很高兴听到它的可能。 – Ryan

-1
https://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Advanced_JPA_Development/Performance/Weaving/Static_Weaving 

尝试的Maven插件从上面的链接。它的工作

+0

虽然这可能在理论上回答这个问题,但[这将是更可取的](// meta.stackoverflow.com/q/8259)在这里包括答案的基本部分,并提供供参考的链接。 –

0

我用invesdwin-instrument来执行。它允许您动态使用加载时织入工具,以便您不必使用任何javaagent。

虽然我花了一些努力使它与Tomcat 8.5一起工作。但它终于可以在Spring Boot中使用这种配置:

@SpringBootApplication 
@EnableLoadTimeWeaving // instead of @ImportResource(locations = "classpath:/META-INF/ctx.spring.weaving.xml") 
public class MySpringBootApplication { 
    public static void main(final String[] args) { 
     DynamicInstrumentationLoader.waitForInitialized(); //dynamically attach java agent to jvm if not already present 
     DynamicInstrumentationLoader.initLoadTimeWeavingContext(); //weave all classes before they are loaded as beans 
     SpringApplication.run(MySpringBootApplication.class, args); //start application, load some classes 
    } 
} 

它也应该可以与以前版本的Tomcat一起使用。

Regards

相关问题