解決策は、EntityListener で Spring の @Configurable アノテーションを使用することです。
理論的には、Spring で管理されていないインスタンス (私の場合は EntityListener) をアスペクト織りにして、このインスタンスが DI を取得できるようにする必要があります。
したがって、さまざまな手順は次のとおりです。
- EntityListener に @Configurable を追加し、注入するフィールドに @Autowired を追加します (TransactionManager here)
<context:spring-configured/>
Spring コンテキストに追加する
- コンパイル時のウィービングを行うには、aspects-maven-plugin を使用します (以下の構成を参照)。
これまでのところうまくいっていますが、私にはうまくいきません。ログには、EntityListerner が織り込まれていることが示されています。
[INFO] Extending interface set for type 'org.project.commons.security.DefaultEntityListener' (DefaultEntityListener.java) to include 'org.springframework.beans.factory.aspectj.ConfigurableObject' (AnnotationBeanConfigurerAspect.aj)
[INFO] Join point 'initialization(void org.springframework.beans.factory.aspectj.ConfigurableObject.<init>())' in Type 'org.project.commons.security.DefaultEntityListener' (DefaultEntityListener.java:38) advised by before advice from 'org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect' (spring-aspects-3.1.2.RELEASE.jar!AbstractDependencyInjectionAspect.class:78(from AbstractDependencyInjectionAspect.aj)) [with runtime test]
[INFO] Join point 'initialization(void org.springframework.beans.factory.aspectj.ConfigurableObject.<init>())' in Type 'org.project.commons.security.DefaultEntityListener' (DefaultEntityListener.java:38) advised by afterReturning advice from 'org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect' (spring-aspects-3.1.2.RELEASE.jar!AbstractDependencyInjectionAspect.class:87(from AbstractDependencyInjectionAspect.aj)) [with runtime test]
[INFO] Join point 'initialization(void org.project.commons.security.DefaultEntityListener.<init>())' in Type 'org.project.commons.security.DefaultEntityListener' (DefaultEntityListener.java:38) advised by afterReturning advice from 'org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect' (spring-aspects-3.1.2.RELEASE.jar!AbstractDependencyInjectionAspect.class:87(from AbstractDependencyInjectionAspect.aj)) [with runtime test]
しかし、期待どおりの注射を受けることはありません。
私は誰でも手がかりを持っています、私はそれを歓迎します...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.4</version>
<configuration>
<showWeaveInfo>true</showWeaveInfo>
<proceedOnError>true</proceedOnError>
<outxml>true</outxml>
<source>1.6</source>
<target>1.6</target>
<complianceLevel>1.6</complianceLevel>
<encoding>${encoding}</encoding>
<aspectLibraries>
<aspectLibrary>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</aspectLibrary>
</aspectLibraries>
<weaveDependencies>
<weaveDependency>
<groupId>org.project</groupId>
<artifactId>commons</artifactId>
</weaveDependency>
</weaveDependencies>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<!--<goal>test-compile</goal>-->
</goals>
</execution>
</executions>
</plugin>