簡體   English   中英

將應用程序TransactionManager注入到JPA EntityListener中

[英]Injecting the application TransactionManager into a JPA EntityListener

我想使用JPA EntityListener來支持Spring Security ACL。 @PostPersist事件上,我創建一個與持久實體相對應的權限。

我需要此操作才能參與當前交易。 為此,我需要在EntityListener引用應用程序TransactionManager

問題是,當EntityManagerFactoryEntityManagerFactory時,Spring無法管理EntityListener因為它是自動創建的。 在經典的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。

當我陷入困境時,任何想法都會有所幫助。

除了nodje的指令之外,您還應該添加一件事-添加AnnotationBeanConfigurerAspect作為實體管理器的依賴項,否則,將在初始化spring上下文之前創建您的實體偵聽器:

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
    depends-on="org.springframework.context.config.internalBeanConfigurerAspect">

一種解決方案是在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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM