在非春季托管类中为DI编译时间编织 [英] Compile time weaving for DI in non-spring managed classes
问题描述
我想为标记为@Configurable
的类配置编译时编织,以便能够将spring依赖项注入到由new
运算符实例化的类中.我不想使用加载时编织,因为我无权运行应用程序服务器的脚本,因此无法对其进行修改.我也希望能够在测试中使用此类,我的意思是从IDE运行测试用例.我只在Web和spring参考上找到有关加载时间编织的信息,而没有关于编译时编织的配置的信息.
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.我在Maven中使用spring
PS. I use spring with maven
推荐答案
因此,其他答案也是有效的,但我想我会更详细地介绍这种方法的一些含义.
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>
我想您会错过用于编织的持久性api.
I suppose that you miss the persistence api, which is used for weaving.
与 https://jira.springsource.org/browse/SPR-6819 春季错误.这似乎就是为什么需要持久性API的原因.
Edit : related to https://jira.springsource.org/browse/SPR-6819 bug in spring. That seems to be why you need the persistence API.
如果在IDE中不编织类,则可以创建一个编织的Maven作业(这对我来说是很常见的事情).
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
最后,如果您打算对类进行单元测试,则在此阶段之后编织类可能会很有用.我们在prepare-package
阶段进行编织.如果您想这样做,请添加
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>
到您的插件.
我希望能对您有所帮助,因为要使这种方法在IDE中很好玩可能会很棘手.
I hope that helps because it can be tricky to get this approach to play nice in the IDE.
这篇关于在非春季托管类中为DI编译时间编织的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!