如何使用 aspectj 在 spring 中的非托管 class 中启用自动装配支持?

How to enable autowire support within unmanaged class in spring using aspectj?

我如何设置 spring / aspectj 以自动装配 classes 中不受 spring classloader 系统管理的 bean?

在我的示例中,我已将 @Configurable 注释添加到我的非托管 class 并设置 aspectj,以便在编译时生成增强的 aspectj classes。我正在使用 JDK 1.8

可在此处找到简约示例的完整源代码: https://github.com/Jotschi/spring-aspecj-compiletime-weaving

在示例中,我还添加了一个自定义方面定义,以验证 aspectj 是否正常工作。 Spring检测到切面,切面处理正确。这样,唯一剩下的就是该字段不会在我的非托管 class.

中自动装配

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>

非托管class:

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;
    }

}

spring配置:

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

解决方案是将 @EnableSpringConfigured 添加到我的 SpringTestConfiguration class。感谢 M. Deinum 指出这一点。