Spring的春季托管bean的加载时编织是否存在冲突?

时间:2013-11-28 17:10:57

标签: spring aop aspectj spring-aop

我的项目已经有一些方面使用<aop:aspectj-autoproxy/>并使用aspectj样式@Aspect注释。但是因为我们需要建议我们控制范围之外的一些类,我们想要引入Tomcat的Spring风格加载时间编织,如http://docs.spring.io/spring/docs/3....tml#aop-aj-ltw

中所述。

我的LTW配置似乎是正确的,因为我打开了详细和调试weaver选项,我在catalina.out中看到这样的行

2013-11-28 13:17:57,167+0000 [INFO ] org.springframework.context.weaving.DefaultContext LoadTimeWeaver [main] - Using a reflective load-time weaver for class loader: org.springframework.instrument.classloading.tomcat .TomcatInstrumentableClassLoader
[TomcatInstrumentableClassLoader@53ea0105] info register aspect com.anon.profiling.aspect.MyAspect
...
[TomcatInstrumentableClassLoader@273d1402] debug not weaving 'org.quartz.SimpleTrigger'
[TomcatInstrumentableClassLoader@273d1402] debug not weaving 'org.apache.http.conn.scheme.SocketFactory'
...

我想要测试的类是在包含其他几个类的包中,我可以看到该包中的其他类被调试的方式与未编织的相同。但是大约有一半的课程(包括我感兴趣的课程......)根本没有被提及 - 要么被编织,要么被忽略或者被忽略。稍微深入研究一下我认为我发现一种模式,被忽略的类由Spring管理:要么在applicationContext.xml中声明,要么用@Inject注入另一个类。

是不是TomcatInstrumentableClassLoader不会加载spring托管bean进行编织?我需要添加到配置中以启用它吗?

- 雷切尔

编辑:响应@gadget

aop.xml文件

<aspectj>

    <!-- "includes" required in order to use both Spring AOP as well as AspectJ Load Time Weaving where Proxied classes are not possible eg. MarkLogicClient -->
    <weaver options="-verbose -showWeaveInfo -Xset:weaveJavaxPackages=true">
        <!-- only weave classes in the following packages -->
        <include within="mycompany..*"/>
        <include within="javax.*"/>
    </weaver>

</aspectj>

的applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

    <context:component-scan base-package="mycompany.appname"/>

    <context:annotation-config/>

    <aop:aspectj-autoproxy proxy-target-class="true"/>

    <context:load-time-weaver/>


    <!-- bean declarations normally here, removed for brevity -->

</beans>

web.xml(道歉,它有点长,但我不想删除任何内容以防万一)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">

    <display-name>appname</display-name>
    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>classpath:log4j.properties</param-value>
    </context-param>
    <servlet>
        <servlet-name>servicestatus</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>4</load-on-startup>
    </servlet>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/*.xml</param-value>
    </context-param>

    <servlet>
        <servlet-name>jersey-servlet</servlet-name>
        <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
        <init-param>
            <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.spi.container.ResourceFilters</param-name>
            <param-value>mycompany.appname.security.ResourceFilterFactory</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.config.property.WadlGeneratorConfig</param-name>
            <param-value>mycompany.appname.jersey.Isite2WadlGeneratorConfig</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>jersey-servlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>servicestatus</servlet-name>
        <url-pattern>/servicestatus</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>webAppRootKey</param-name>
        <param-value>logging</param-value>
    </context-param>

    <context-param>
        <param-name>propertiesConfigLocation</param-name>
        <param-value>classpath:spring/propertiesConfigContext.xml</param-value>
    </context-param>

    <filter>
        <filter-name>utf8ValidatingFilter</filter-name>
        <filter-class>mycompany.appname.filter.Utf8ValidatingFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>utf8ValidatingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter>
        <filter-name>timingFilter</filter-name>
        <filter-class>mycompany.appname.filter.TimingFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>timingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <listener>
        <listener-class>mycompany.appname.log.EnvironmentalPropertiesLog4jContextListener</listener-class>
    </listener>

</web-app>

0 个答案:

没有答案