Compile temporary weaving for DI in spring managed classes

I want to set compile time for classes marked with @Configurable annotation to embed spring dependencies on classes set with the new operator. I do not want to use binding time, because I do not have access to run the application server script, so I can not modify it. I also want to be able to use these classes in tests, I want to run test cases from the IDE. I found the information only about downloading temporary weaving on the Internet and spring reference information and nothing about setting compile time.

PS. I am using spring with maven

+6
source share
2 answers

So the other answer is also valid, but I thought I would talk a little more about some of the implications of this approach.

The setup I'm using is at a basic level: -

  <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, more information about 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 assume that you will miss the persistence api, which is used for weaving.

Edit: associated with https://jira.springsource.org/browse/SPR-6819 error in spring. It looks like you need the persistence API for this.

It is also useful to create a maven task to weave if classes are not decoupled in ide (this is very common for me).

 aspectj:compile 

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

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

In your plugin.

I hope this helps, because it can be difficult to make this approach play well in the IDE.

+4
source

Something like this in your pom should work ...

  <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>aspectj-maven-plugin</artifactId> <version>1.4</version> <executions> <execution> <id>compile</id> <configuration> <source>${source}</source> <target>${target}</target> <verbose>true</verbose> <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>${source}</source> <target>${target}</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> <!-- Ensure aspectj tools version used by compiler is the same version used as dependency. Avoids warning --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjtools</artifactId> <version>${aspectj.version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>${aspectj.version}</version> </dependency> </dependencies> </plugin> 

Then, make sure you have spring aspects on the way to your class.

And for aspecting in Eclipse - install AJDT

+1
source

Source: https://habr.com/ru/post/950354/


All Articles