Why doesn't AspectJ compile-time weaving of Spring's @Configurable work?

后端 未结 4 1501
后悔当初
后悔当初 2020-12-02 13:51

Update 5: I\'ve downloaded the latest Spring ToolsSuite IDE based on the latest Eclipse. When I import my project as a Maven project, Eclipse/STS appears to

相关标签:
4条回答
  • 2020-12-02 14:16

    It works for us on maven using compile time weaving, try adding the following plugins:

    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <compilerVersion>1.6</compilerVersion>
        <fork>true</fork>
        <source>1.6</source>
        <target>1.6</target>
    </configuration>
    </plugin>
    
    <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>compile</id>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
                <verbose>false</verbose>
                <outxml>true</outxml>
                <aspectLibraries>
                    <aspectLibrary>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-aspects</artifactId>
                    </aspectLibrary>
                </aspectLibraries>
            </configuration>
            <goals>
                <goal>compile</goal>
            </goals>
        </execution>
        <execution>
            <id>test-compile</id>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
                <verbose>false</verbose>
                <aspectLibraries>
                    <aspectLibrary>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-aspects</artifactId>
                    </aspectLibrary>
                </aspectLibraries>
            </configuration>
            <goals>
                <goal>test-compile</goal>
            </goals>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.6.4</version>
        </dependency>
    </dependencies>
    </plugin>
    

    Its done as two separate execution steps to allow you to add different aspect libraries for unit testing and compilation.

    You'll also need the following dependency added for the spring-aspects library:

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <scope>compile</scope>
        </dependency>
    
    0 讨论(0)
  • 2020-12-02 14:16

    I successfully configured load-time weaving in my app, if this is an alternative for you.

    My environment:

    • JDK-1.6
    • Spring-2.5.6
    • JPA with eclipselink-1.1.0

    Configuration details:

    Spring XML configuration:

    <context:annotation-config/>
    <context:spring-configured/>
    <context:load-time-weaver/>
    
    <bean id="baseEntity" class="package.name.BaseEntity" scope="prototype">
      <property name="historyHandler" ref="historyHandler" />
    </bean>
    
    <bean id="historyHandler" class="package.name.HistoryJpaHandler" scope="prototype">
      <property name="historyDao" ref="historyDao" />
    </bean>
    
    <bean id="historyDao" class="package.name.HistoryJpaDao">
      <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>
    

    Spring annotations

    @Configurable("baseEntity")
    public abstract class BaseEntity
    
    @Configurable("historyHandler")
    public class HistoryJpaHandler extends SessionEventAdapter implements HistoryHandler 
    

    Java VM Parameter

    <JAVA_HOME>/bin/java -javaagent:/full/path/to/spring-agent-2.5.6.jar
    

    Instances of historyHandler and baseEntitty are created by ecliselink. historyHandler in baseEntitty and historyDao in historyHandler is set by load-timeweaving.

    You can set the VM Parameter in Eclipse run configuration or in Tomcats catalina.sh/bat.

    0 讨论(0)
  • 2020-12-02 14:24

    making a field of a @configurable class Autowired throws NullPointerException if you do not configure your spring properly for this annotation. follow these steps to make @configurable annotations work properly

    This method is called AspectJ build time weaving to inject spring beans to your non-spring-made classes.

    First step is to install these plugins in eclipse:

    From these two update sites install whatever eclipse suggests:

    http://download.eclipse.org/tools/ajdt/43/update
    http://dist.springsource.org/release/AJDT/configurator/ 
    

    After installing,right-click on project and Do:

    Configure > Convert to Aspectj
    Maven > Update
    

    Next, you need to add these to your pom.xml:

    Under Dependencies Add:

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>4.0.2.RELEASE</version>
    </dependency>
    

    Under Plugins Add:

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.5</version>
                <configuration>
                    <showWeaveInfo>true</showWeaveInfo>
                    <source>1.7</source>
                    <target>1.7</target>
                    <Xlint>ignore</Xlint>
                    <complianceLevel>1.7</complianceLevel>
                    <encoding>UTF-8</encoding>
                    <verbose>false</verbose>
                    <aspectLibraries>
                        <aspectLibrary>
                            <groupId>org.springframework</groupId>
                            <artifactId>spring-aspects</artifactId>
                        </aspectLibrary>
                    </aspectLibraries>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjrt</artifactId>
                        <version>1.7.0</version>
                    </dependency>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjtools</artifactId>
                        <version>1.7.0</version>
                    </dependency>
                </dependencies>
            </plugin>
    

    Important: DO NOT use any <pluginManagment> tag under <build> tag. your pom.xml needs to be something like this:

    <project ....>
        ....
        <dependencies>
            <dependency> 
                        ....
            </dependency>
                    ....
        </dependencies>
        <build>
            <plugins>
                <plugin>
                                ....
                </plugin>
                            ....
            </plugins>
        </build>
    </project>
    

    finally add <context:spring-configured /> to your spring application context config file.

    Now you can annotate a POJO class as @Configurable and inject spring beans in it using @Autowired annotation. this way whenever you make a new instance of that POJO it will be configured (e.g. injected with dependencies) automatically.

    0 讨论(0)
  • 2020-12-02 14:24

    As far as your Eclipse classpath issues are concerned, you might find this useful.

    The m2eclipse plugin has an optional AJDT integration. The integration reads the aspectLibraries section of the aspectj-maven-plugin's configuration, and contributes the jars to Eclipse's Aspect Path.

    0 讨论(0)
提交回复
热议问题