将应用程序TransactionManager注入JPA EntityListener

时间:2012-08-21 17:03:59

标签: java spring security jpa acl

我想使用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。

任何想法都会有所帮助,因为我被困在想法中。

2 个答案:

答案 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。

所以这是不同的步骤:

  • 将@Configurable添加到EntityListener并将@Autowired添加到要注入的字段(此处为TransactionManager)
  • <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>