2016-03-14 20 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

相關問題