如何配置Spring为Transactional使用aspectj?

时间:2015-11-09 13:30:15

标签: java spring maven proxy aspectj

根据这个答案 https://stackoverflow.com/a/3429757/2674303 要在执行本地方法时使用@Transactional,我执行了以下步骤:

添加到pom.xml:

    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.8.7</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.8.7</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
    </dependency>

替换上下文

<tx:annotation-driven transaction-manager="transactionManager"/>

<tx:annotation-driven transaction-manager="transactionManager" mode="aspectj"/>
之后我看到了

  

无法获取当前线程的事务同步会话

另外补充说:

<context:load-time-weaver/>

但是当我看到make mvn clean install时(当测试尝试执行时会出现问题。在我的测试中我提出了应用上下文):

    java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:94)
    at org.springframework.test.context.DefaultTestContext.getApplicationContext(DefaultTestContext.java:72)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
    ....
Caused by: 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 [sun.misc.Launcher$AppClassLoader] 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
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:547)

    ... 31 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loadTimeWeaver': Initialization of bean failed; nested exception is java.lang.IllegalStateException: ClassLoader [sun.misc.Launcher$AppClassLoader] 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
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:547)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)

    ... 45 more
Caused by: java.lang.IllegalStateException: ClassLoader [sun.misc.Launcher$AppClassLoader] 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
    at org.springframework.context.weaving.DefaultContextLoadTimeWeaver.setBeanClassLoader(DefaultContextLoadTimeWeaver.java:91)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeAwareMethods(AbstractAutowireCapableBeanFactory.java:1590)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1561)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
    ... 54 more

更新

整个pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">


    <modelVersion>4.0.0</modelVersion>
    <groupId>com.terminal</groupId>
    <artifactId>terminal-company</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>terminal-company</name>


    <properties>
        <spring.version>4.1.6.RELEASE</spring.version>
        <spring.security.version>3.2.4.RELEASE</spring.security.version>
        <org.slf4j-version>1.5.10</org.slf4j-version>
        <jetty.version>9.2.5.v20141112</jetty.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-framework-bom</artifactId>
                <version>${spring.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.security</groupId>
                <artifactId>spring-security-bom</artifactId>
                <version>${spring.security.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <!-- Web -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.el</groupId>
            <artifactId>el-api</artifactId>
            <version>2.2</version>
        </dependency>

        <!-- Data -->
        <dependency>
            <groupId>postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.0-801.jdbc4</version>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.182</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
        </dependency>

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.8.0</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.0</version>
        </dependency>

        <dependency>
            <groupId>javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.12.0.GA</version>
        </dependency>

        <!-- Spring Security -->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-taglibs</artifactId>
        </dependency>
        <!-- Integration-->
        <dependency>
            <groupId>org.springframework.integration</groupId>
            <artifactId>spring-integration-http</artifactId>
            <version>${spring.version}</version>
        </dependency>


        <!-- Hibernate -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.3.10.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.1.3.Final</version>
        </dependency>

        <!-- logging -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${org.slf4j-version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>${org.slf4j-version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${org.slf4j-version}</version>
        </dependency>

        <!-- JSON -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.2.2</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.2.2</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.2.2</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.12</version>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20140107</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>1.7.1</version>
        </dependency>

        <!-- Commons -->
        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.8.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.1</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.8</version>
        </dependency>
        <dependency>
            <groupId>commons-httpclient</groupId>
            <artifactId>commons-httpclient</artifactId>
            <version>3.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.0.2</version>
        </dependency>
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4</version>
        </dependency>
        <!-- Test -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>1.10.19</version>
            <scope>test</scope>
        </dependency>


        <!-- other -->
        <dependency>
            <groupId>org.imgscalr</groupId>
            <artifactId>imgscalr-lib</artifactId>
            <version>4.2</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>net.sourceforge.pmd</groupId>
            <artifactId>pmd-jsp</artifactId>
            <version>5.2.1</version>
        </dependency>
        <dependency>
            <groupId>net.sourceforge.pmd</groupId>
            <artifactId>pmd-core</artifactId>
            <version>5.2.1</version>
        </dependency>
        <dependency>
            <groupId>net.sourceforge.pmd</groupId>
            <artifactId>pmd-java</artifactId>
            <version>5.2.1</version>
        </dependency>
        <dependency>
            <groupId>xml-apis</groupId>
            <artifactId>xml-apis</artifactId>
            <version>1.4.01</version>
        </dependency>
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.4</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity</artifactId>
            <version>1.7</version>
        </dependency>
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-tools</artifactId>
            <version>2.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tika</groupId>
            <artifactId>tika-core</artifactId>
            <version>1.9</version>
        </dependency>
        <dependency>
            <groupId>net.bull.javamelody</groupId>
            <artifactId>javamelody-core</artifactId>
            <version>1.56.0</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.8.7</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.7</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
        </dependency>
    </dependencies>
    <build>
        <finalName>terminal-company</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-pmd-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <includeTests>true</includeTests>
                    <printFailingErrors>true</printFailingErrors>
                    <verbose>true</verbose>
                    <rulesets>
                        <ruleset>rulesets/java/basic.xml</ruleset>
                        <ruleset>rulesets/java/braces.xml</ruleset>
                        <ruleset>rulesets/java/unusedcode.xml</ruleset>
                        <!--<ruleset>rulesets/java/codesize.xml</ruleset>-->
                        <ruleset>rulesets/java/imports.xml</ruleset>
                        <ruleset>rulesets/java/empty.xml</ruleset>
                        <ruleset>rulesets/jsp/basic.xml</ruleset>
                        <ruleset>ruleset-naming.xml</ruleset>
                    </rulesets>
                    <includes>
                        <include>**\/*.jsp</include>
                        <include>**\/*.java</include>
                    </includes>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>check</goal>
                        </goals>
                        <configuration>
                            <excludeFromFailureFile>${basedir}/src/main/resources/exclude-pmd.properties
                            </excludeFromFailureFile>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.1.1</version>
                <configuration>
                    <warName>terminal-company</warName>
                    <webXml>src/main/webapp/WEB-INF/web.xml</webXml>
                    <webResources>
                        <resource>
                            <directory>${basedir}/src/main/resources/</directory>
                            <targetPath>/</targetPath>
                        </resource>
                    </webResources>
                </configuration>
            </plugin>
            <!-- IDE -->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>tomcat-maven-plugin</artifactId>
                <version>1.1</version>
            </plugin>
            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>${jetty.version}</version>
                <configuration>
                    <webAppConfig>
                        <contextPath>/</contextPath>
                    </webAppConfig>
                    <httpConnector>
                        <port>8081</port>
                    </httpConnector>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.3</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>org.mortbay.jetty</groupId>
                                    <artifactId>jetty-runner</artifactId>
                                    <version>8.1.15.v20140411</version>
                                    <destFileName>jetty-runner.jar</destFileName>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.flywaydb</groupId>
                <artifactId>flyway-maven-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <url>jdbc:postgresql://127.0.0.1:5432/marc_db</url>
                    <user>marc</user>
                    <password>marc</password>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.postgresql</groupId>
                        <artifactId>postgresql</artifactId>
                        <version>9.4-1200-jdbc4</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
    <repositories>
        <repository>
            <id>central</id>
            <name>Maven Repository Switchboard</name>
            <layout>default</layout>
            <url>http://repo1.maven.org/maven2</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-maven-release</id>
            <name>Spring Maven Release Repository</name>
            <url>http://maven.springframework.org/release</url>
        </repository>
        <repository>
            <id>spring-maven-milestone</id>
            <name>Spring Maven Milestone Repository</name>
            <url>http://maven.springframework.org/milestone</url>
        </repository>
        <repository>
            <id>JBoss Repo</id>
            <url>https://repository.jboss.org/nexus/content/repositories/releases</url>
            <name>JBoss Repo</name>
        </repository>
        <repository>
            <id>codelds</id>
            <url>https://code.lds.org/nexus/content/groups/main-repo</url>
        </repository>
        <repository>
            <id>glassfish-repository</id>
            <url>http://download.java.net/maven/glassfish</url>
            <snapshots>
                <updatePolicy>never</updatePolicy>
            </snapshots>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-maven-release</id>
            <name>Spring Maven Release Repository</name>
            <url>http://maven.springframework.org/release</url>
        </pluginRepository>
        <pluginRepository>
            <id>spring-maven-milestone</id>
            <name>Spring Maven Milestone Repository</name>
            <url>http://maven.springframework.org/milestone</url>
        </pluginRepository>
        <pluginRepository>
            <id>spring-roo-repository</id>
            <name>Spring Roo Repository</name>
            <url>http://spring-roo-repository.springsource.org/release</url>
        </pluginRepository>
        <pluginRepository>
            <id>central</id>
            <name>Maven Plugin Repository</name>
            <url>http://repo1.maven.org/maven2</url>
            <layout>default</layout>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
            <releases>
                <updatePolicy>never</updatePolicy>
            </releases>
        </pluginRepository>
    </pluginRepositories>
</project>

更新2:

如果在构建应用程序时跳过测试阶段 - 构建成功,然后运行mvn clean install jetty:run,我会看到以下stacktrace:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.config.internalTransactionAspect': Post-processing failed of bean type [class org.springframework.transaction.aspectj.AnnotationTransactionAspect] failed; nested exception is java.lang.IllegalStateException: Failed to introspect annotations: protected abstract void org.springframework.transaction.aspectj.AbstractTransactionAspect.ajc$pointcut$$transactionalMethodExecution$e12(java.lang.Object)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyMergedBeanDefinitionPostProcessors(AbstractAutowireCapableBeanFactory.java:936)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:512)
    ...
Caused by: java.lang.IllegalStateException: Failed to introspect annotations: protected abstract void org.springframework.transaction.aspectj.AbstractTransactionAspect.ajc$pointcut$$transactionalMethodExecution$e12(java.lang.Object)
    at org.springframework.core.annotation.AnnotatedElementUtils.process(AnnotatedElementUtils.java:166)
    ... 69 more
Caused by: java.lang.IllegalAccessError: class sun.reflect.GeneratedConstructorAccessor14 cannot access its superclass sun.reflect.ConstructorAccessorImpl
    at sun.misc.Unsafe.defineClass(Native Method)
    at sun.reflect.ClassDefiner.defineClass(ClassDefiner.java:63)
    ... 76 more
13.11.2015 04:57:35 ERROR: org.springframework.web.context.ContextLoader - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.config.internalTransactionAspect': Post-processing failed of bean type [class org.springframework.transaction.aspectj.AnnotationTransactionAspect] failed; nested exception is java.lang.IllegalStateException: Failed to introspect annotations: protected abstract void org.springframework.transaction.aspectj.AbstractTransactionAspect.ajc$pointcut$$transactionalMethodExecution$e12(java.lang.Object)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyMergedBeanDefinitionPostProcessors(AbstractAutowireCapableBeanFactory.java:936)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:512)
    ...
Caused by: java.lang.IllegalStateException: Failed to introspect annotations: protected abstract void org.springframework.transaction.aspectj.AbstractTransactionAspect.ajc$pointcut$$transactionalMethodExecution$e12(java.lang.Object)
    at org.springframework.core.annotation.AnnotatedElementUtils.process(AnnotatedElementUtils.java:166)
    at org.springframework.core.annotation.AnnotatedElementUtils.getAnnotationAttributes(AnnotatedElementUtils.java:91) 
    ... 69 more
Caused by: java.lang.IllegalAccessError: class sun.reflect.GeneratedConstructorAccessor14 cannot access its superclass sun.reflect.ConstructorAccessorImpl
    at sun.misc.Unsafe.defineClass(Native Method)   
    ... 76 more

更新3

我也尝试添加

@Configuration
@EnableLoadTimeWeaving(aspectjWeaving= EnableLoadTimeWeaving.AspectJWeaving.ENABLED)
public class Configiuration {

我看到同样的结果

更新4:

使用Transactional注释的类:

@Service
@Transactional
public class CompanyServiceImpl implements CompanyService {
    @PostConstruct
    public void init() {
        this.refreshStopJobs();
    }
    ...
}   

解决方案

实际上this post对我很有帮助

3 个答案:

答案 0 :(得分:4)

基本上,要使AspectJ加载时间编织工作,您首先需要考虑您的应用程序将要运行的环境。正如名称所说的'#34;加载时间&#34;意味着您的类将由类加载器更改。所以从你的问题我明白你需要你的LTW在两个环境中。首先是&#34;测试&#34;命令行的环境和第二个是Servlet容器(Jetty / Tomcat)。

所以让我们从测试环境开始:你的测试失败了,因为类加载器没有机会编辑/仪器/编织你的类,Spring无法帮助你,因为它还没有运行而你选择了加载时间超过运行时编织。所以我们需要以某种方式告诉默认的类加载器我们将要破解类。这在Spring Documentation中有记录。为此,我们需要添加-javaagent,在我们的情况下,而不是检测/分析运行时只会在加载时更改类。要实现此目的,只需告诉maven添加2 javaaganetsaspectj-weaver-x.jar和特殊spring-instrument-x.jar,所以只需在您的maven plugins部分添加。

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.19</version>
        <configuration>
            <forkMode>once</forkMode>
            <argLine>
                    -javaagent:${settings.localRepository}/org/springframework/spring-instrument/4.2.2.RELEASE/spring-instrument-4.1.6.RELEASE.jar
                    -javaagent:${settings.localRepository}/org/aspectj/aspectjweaver/1.8.7/aspectjweaver-1.8.7.jar
            </argLine>
            <useSystemClassloader>true</useSystemClassloader>
        </configuration>
</plugin>

请记住至少在spring-instrument-4.1.6.RELEASE.jar范围内添加provided作为依赖项,以确保它位于repo中;) 添加此配置设置后,测试执行将正常工作。试试吧:

mvn clean package

接下来让我们从Servlet容器的第二个环境开始。 如果您正在运行Tomcat,那么您很高兴,因为您可以通过向项目添加以下META-INF/context.xml来覆盖弹簧附带的默认类加载器。

<Context path="/myWebApp" docBase="/my/webApp/location">
    <Loader
    loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader"/>
</Context>

更多信息here。对于Jetty,您应该手动编辑运行脚本并添加两个-javaagent,就像我们为maven测试环境所做的那样。

答案 1 :(得分:0)

在您使用的XML配置中

<context:load-time-weaver/>

但是使用默认模式autodect加载时,只有拥有META-INF / aop.xml文件时才会进行编织。由于您似乎没有,您应该将其更改为

<context:load-time-weaver aspectj-weaving="on"/>

或添加META-INF / aop.xml。 我看到你在Java配置中做到了这一点,但看起来你是XML的大部分配置。

答案 2 :(得分:0)

您可能希望参考这个有用的链接:

Spring 2.0 AOP - Spring 2.0引入了一种使用基于模式的方法或 @AspectJ 注释样式编写自定义方面的更简单,更强大的方法。这两种样式都提供完全类型的建议和AspectJ切入点语言的使用,同时仍然使用Spring AOP进行编织。

本章将讨论基于Spring 2.0模式和@ AspectJ的AOP支持。 Spring 2.0 AOP仍然完全向后兼容Spring 1.2 AOP,Spring 1.2 API提供的低级AOP支持将在下一章中讨论。

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html