1

春のセキュリティ ACL をサポートするために JPA EntityListener を使用したいと考えています。イベント@PostPersistでは、永続エンティティに対応するアクセス許可を作成します。

現在のトランザクションに参加するには、この操作が必要です。これを行うには、アプリケーションへの参照が必要TransactionManagerですEntityListener

問題は、インスタンス化EntityListenerされたときに自動的に作成されるため、Spring が を管理できないことです。EntityManagerFactoryまた、従来の Spring アプリでは、インスタンス化EntityManagerFactory中に 自体が作成されます。TransactioManager

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

TransactionManagerそのため、まだインスタンス化されていないため、コンストラクターでを注入する方法がありません。

の別のインスタンスを作成しEntityManagerます。InitiliazingBean を実装して使用しても、Spring マネージド Bean ではないため機能しません。@ComponentEntityManagerafterPropertySet()

私は立ち往生していてアイデアがないので、どんなアイデアも役に立ちます。

4

2 に答える 2

1

nodjeの指示に加えて、もう1つ追加する必要があります。エンティティマネージャーの依存関係としてAnnotationBeanConfigurerAspectを追加します。そうしないと、Springコンテキストが初期化される前にエンティティリスナーが作成されます。

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
    depends-on="org.springframework.context.config.internalBeanConfigurerAspect">
于 2012-09-20T15:36:09.437 に答える
0

解決策は、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>
于 2012-08-22T16:04:13.293 に答える