Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile time weaving for DI in non-spring managed classes

I want to configure compile time weaving for classes marked with @Configurable annotation to be able to inject spring dependencies to classes instatiated with new operator. I don't want to use load-time weaving because I don't have access to run script of application server, so I can't modify it. Also I want to be able to use this classes in tests, I mean to run test cases from IDE . I found information only about load time weaving on the web and spring reference and nothing about configuration of compile-time weaving.

PS. I use spring with maven

like image 391
maks Avatar asked Jul 26 '13 13:07

maks


Video Answer


1 Answers

So the other answer is also valid but I thought i'd cover in a little more detail some of the implications of this approach.

The setup I use is at a basic level this :-

      <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.4</version>
            <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>
            <configuration>
                <outxmlfile>META-INF/aop.xml</outxmlfile>
                <aspectLibraries>
                    <aspectLibrary>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-aspects</artifactId>
                    </aspectLibrary>
                </aspectLibraries>
                <source>1.7</source>
                <target>1.7</target>
                 <forceAjcCompile>true</forceAjcCompile>
            </configuration>
        </plugin>

Now, some additional information about the dependencies :-

 <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>${dep.spring}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>${dep.spring}</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>javax.persistence</groupId>
        <artifactId>persistence-api</artifactId>
        <version>1.0</version>
        <scope>provided</scope>
    </dependency>

I suppose that you miss the persistence api, which is used for weaving.

Edit : related to https://jira.springsource.org/browse/SPR-6819 bug in spring. That seems to be why you need the persistence API.

Also helpful can be to create a maven job to weave if classes get unweaved in the ide (this happens a lot for me).

aspectj:compile

Finally if you intend to unit test your classes, it can be useful to weave classes after this phase. We weave in the prepare-package phase. If you would like to do this add

      <executions>
                <execution>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
            </executions>

To your plugin.

I hope that helps because it can be tricky to get this approach to play nice in the IDE.

like image 182
MikePatel Avatar answered Oct 05 '22 17:10

MikePatel