如何设置spring / aspectj以在不由spring类加载器系统管理的类中自动装配bean?
在我的示例中,我已将@Configurable注释添加到我的非托管类并设置aspectj,以便在编译期间生成aspectj增强类。我正在使用JDK 1.8
可以在此处找到简约示例的完整源代码: https://github.com/Jotschi/spring-aspecj-compiletime-weaving
在示例中,我还添加了自定义方面定义,以验证aspectj是否正常工作。 Spring已检测到方面并且方面已正确处理。这样,唯一剩下的就是该字段在我的非托管类中没有自动装配。
的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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.jotschi</groupId>
<artifactId>spring-aspecj-compiletime-weaving</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<spring.version>4.1.6.RELEASE</spring.version>
<aspectj.version>1.8.5</aspectj.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
<!-- Test dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.7</version>
<configuration>
<outxmlfile>META-INF/aop.xml</outxmlfile>
<aspectLibraries>
<aspectLibrary>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</aspectLibrary>
</aspectLibraries>
<complianceLevel>1.8</complianceLevel>
<source>1.8</source>
<target>1.8</target>
<forceAjcCompile>true</forceAjcCompile>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
非托管类:
package de.jotschi.test;
import static org.junit.Assert.assertNotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
@Configurable
public class UnmanagedClass {
@Autowired
private MessagerService messagerService;
public UnmanagedClass() {
// TODO Auto-generated constructor stub
}
public void test() {
System.out.println("test called");
assertNotNull("The service within the " + getClass().getSimpleName()
+ " class should not be null since the class was annotated with @Configurable and the field was autowired.", messagerService);
messagerService.sayHello();
}
public void setService(MessagerService messagerService) {
this.messagerService = messagerService;
}
}
弹簧配置:
package de.jotschi.test;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@ComponentScan(basePackages = { "de.jotschi" })
@EnableAspectJAutoProxy
public interface SpringTestConfiguration {
}
更新
我更新了我的github项目。它现在包含缺少的注释,可能对想要设置aspectj + spring的人有用。
https://github.com/Jotschi/spring-aspecj-compiletime-weaving
答案 0 :(得分:0)
解决方案是将@EnableSpringConfigured
添加到我的SpringTestConfiguration
课程中。感谢M. Deinum指出这一点。