我想使用JPA EntityListener来支持spring安全ACL。
在@PostPersist
事件上,我创建了与持久化实体相对应的权限。
我需要此操作才能参与当前的交易。
要做到这一点,我需要在TransactionManager
中引用应用EntityListener
。
问题是,Spring无法管理EntityListener
,因为它是在EntityManagerFactory
实例化时自动创建的。
在经典的Spring应用程序中,EntityManagerFactory
本身是在TransactioManager
实例化期间创建的。
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
所以我无法用构造函数注入TransactionManager
,因为它尚未实例化。
使EntityManager
成为@Component
创建EntityManager
的另一个实例。
实现InitiliazingBean并使用afterPropertySet()
不起作用,因为它不是Spring托管bean。
任何想法都会有所帮助,因为我被困在想法中。
答案 0 :(得分:1)
除了nodje的指令之外,你还应该添加一个东西 - 添加AnnotationBeanConfigurerAspect作为你的实体管理器的依赖,否则你的实体监听器将在春天上下文初始化之前创建:
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
depends-on="org.springframework.context.config.internalBeanConfigurerAspect">
答案 1 :(得分:0)
解决方案是在EntityListener上使用Spring的@Configurable批注。
理论上它应该允许非Spring托管实例(在我的情况下是EntityListener)进行方面编织,从而允许此实例获得DI。
所以这是不同的步骤:
<context:spring-configured/>
添加到Spring上下文到目前为止一直很好,但它对我不起作用。日志说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>