25

更新 5:最新の Eclipse ベースの最新の Spring ToolsSuite IDE をダウンロードしました。プロジェクトを Maven プロジェクトとしてインポートすると、Eclipse/STS はプロジェクトのビルドに Maven ゴールを使用しているように見えます。これは、AspectJ が最終的に Eclipse で正しく動作することを意味します。

更新 4: Eclipse のメカニズムを効果的にバイパスして、コンパイル時のウィービングに Maven + AspectJ プラグインを使用するだけになりました。

更新 3: AspectJ の Eclipse プラグインが、Tomcat に正しく公開する Eclipse の機能を壊しているようです。プロジェクトの AspectJ 機能を削除することによってのみ、プロジェクトを適切に公開できるようになります。とてもうるさい。

更新 2:これが Eclipse で動作するようになりました。これを言うのは非常に不快ですが、EclipseまたはMavenビルドからどのように機能するようになったのかわかりません。実行時の問題ではなく、コンパイルの問題のようです。

更新 1: Maven ビルドを介して動作するようになったようですが、方法がわかりません。Eclipse はまだ動作しません。pom.xmlで変更したのは、これらの (重要でない?) 構成パラメーターを追加したことだけです。

<source>1.6</source>
<complianceLevel>1.6</complianceLevel>
<verbose>true</verbose>
<showWeaveInfo>true</showWeaveInfo>
<outxml>true</outxml>

私は実際に、すべてが一貫して機能しないこの問題が繰り返されることを心配しています。詳細がわかり次第、この質問を更新していきます。

Eclipse に関しては、組み込みたいバイナリ アスペクト (この場合はspring-aspects.jar ) をクラスパスからコピーすることで、ある程度の進歩を遂げました。次に、この外部 jar をAspect Pathに追加します。これを行った後、Eclipse はコード内に AspectJ マーカーを適切に表示します。Mavenプラグインを介してMavenによって維持されているJavaビルドパスにspring-aspects.jarをそのままにしておくことができないのは面倒です。ただし、何らかの理由で、バイナリ アスペクトがAspect Pathに明示的に追加されない限り、AspectJ プラグインはバイナリ アスペクトを認識しません。


元の投稿: @Configurable は、Spring の外部でインスタンス化されたオブジェクトに依存関係を注入できるようにする Spring アノテーションです (たとえば、Hibernate またはいくつかの Factory クラスによって)。

以前、この注釈をロード時の織りで使用していましたが、ほとんど機能していました。時々、起動しても何も注入されませんでした。この問題により、この StackOverflow questionが発生しました。多くの回答はありませんでしたが、信頼性が高いため、代わりにコンパイル時のウィービングを試すことを提案する人がほとんどでした。

Eclipse および Maven 用の AspectJ プラグインをインストールしました。これらは両方とも、適切にコンパイルされたクラスのように見えるものを生成します。AspectJ をコンパイルする前に、クラスの 1 つをテキスト エディターで開いたところ、AspectJ への参照が見つかりませんでした。AspectJ のコンパイル後に開いたところ、Eclipse と Maven の両方で生成されたバージョンにorg.aspectj.weaver.MethodDeclarationLineNumberへの参照があります。これが、適切にコンパイルされていると想定する理由です。問題は、デプロイされると、依存関係が注入されないことです。

私のSpring applicationContext.xmlには以下が含まれています:

    <context:spring-configured />

    <context:component-scan base-package="com.myapp" />

@Configurable とマークされたクラスが DI を実行するために必要なのは、上記のすべてですか? ロード時ウィービングからコンパイル時ウィービングへの変換中に、META-INF/aop.xml<context:load-time-weaver />applicationContext.xmlから削除し、Spring の Tomcat ウィーバーをcontext.xmlから削除しました。

この問題をさらに調査するにはどうすればよいですか? 考えられる原因は何ですか?

4

4 に答える 4

26

次のプラグインを追加してみてください。

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
    <compilerVersion>1.6</compilerVersion>
    <fork>true</fork>
    <source>1.6</source>
    <target>1.6</target>
</configuration>
</plugin>

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<executions>
    <execution>
        <id>compile</id>
        <configuration>
            <source>1.6</source>
            <target>1.6</target>
            <verbose>false</verbose>
            <outxml>true</outxml>
            <aspectLibraries>
                <aspectLibrary>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-aspects</artifactId>
                </aspectLibrary>
            </aspectLibraries>
        </configuration>
        <goals>
            <goal>compile</goal>
        </goals>
    </execution>
    <execution>
        <id>test-compile</id>
        <configuration>
            <source>1.6</source>
            <target>1.6</target>
            <verbose>false</verbose>
            <aspectLibraries>
                <aspectLibrary>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-aspects</artifactId>
                </aspectLibrary>
            </aspectLibraries>
        </configuration>
        <goals>
            <goal>test-compile</goal>
        </goals>
    </execution>
</executions>
<dependencies>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.6.4</version>
    </dependency>
</dependencies>
</plugin>

単体テストとコンパイル用に異なるアスペクト ライブラリを追加できるように、2 つの個別の実行ステップとして実行されます。

また、spring-aspects ライブラリに次の依存関係を追加する必要があります。

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <scope>compile</scope>
    </dependency>
于 2009-05-28T12:49:45.773 に答える
7

これが代替手段である場合、アプリで読み込み時のウィービングを正常に構成しました。

私の環境:

  • JDK-1.6
  • 春-2.5.6
  • JPA と eclipselink-1.1.0

構成の詳細:

春の XML 構成:

<context:annotation-config/>
<context:spring-configured/>
<context:load-time-weaver/>

<bean id="baseEntity" class="package.name.BaseEntity" scope="prototype">
  <property name="historyHandler" ref="historyHandler" />
</bean>

<bean id="historyHandler" class="package.name.HistoryJpaHandler" scope="prototype">
  <property name="historyDao" ref="historyDao" />
</bean>

<bean id="historyDao" class="package.name.HistoryJpaDao">
  <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

春の注釈

@Configurable("baseEntity")
public abstract class BaseEntity

@Configurable("historyHandler")
public class HistoryJpaHandler extends SessionEventAdapter implements HistoryHandler 

Java VM パラメータ

<JAVA_HOME>/bin/java -javaagent:/full/path/to/spring-agent-2.5.6.jar

historyHandler および baseEntitty のインスタンスは、ecriselink によって作成されます。baseEntitty の historyHandler と historyHandler の historyDao は、load-timeweaving によって設定されます。

Eclipse 実行構成または Tomcats catalina.sh/bat で VM パラメータを設定できます。

于 2009-05-28T08:59:13.850 に答える
4

@configurable クラス Autowired のフィールドを作成すると、このアノテーションに対してスプリングを適切に構成しないと、NullPointerException がスローされます。@configurable アノテーションを適切に機能させるには、次の手順に従います

この方法は、Spring Bean を非 Spring 製クラスに注入するための AspectJ ビルド時織りと呼ばれます。

最初のステップは、これらのプラグインを Eclipse にインストールすることです。

これらの 2 つの更新サイトから、Eclipse が提案するものをインストールします。

http://download.eclipse.org/tools/ajdt/43/update
http://dist.springsource.org/release/AJDT/configurator/ 

インストール後、プロジェクトを右クリックして次を実行します。

Configure > Convert to Aspectj
Maven > Update

次に、これらを pom.xml に追加する必要があります。

依存関係の下に以下を追加します。

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
    <version>4.0.2.RELEASE</version>
</dependency>

プラグインの下に次を追加します。

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.5</version>
            <configuration>
                <showWeaveInfo>true</showWeaveInfo>
                <source>1.7</source>
                <target>1.7</target>
                <Xlint>ignore</Xlint>
                <complianceLevel>1.7</complianceLevel>
                <encoding>UTF-8</encoding>
                <verbose>false</verbose>
                <aspectLibraries>
                    <aspectLibrary>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-aspects</artifactId>
                    </aspectLibrary>
                </aspectLibraries>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                        <goal>test-compile</goal>
                    </goals>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjrt</artifactId>
                    <version>1.7.0</version>
                </dependency>
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjtools</artifactId>
                    <version>1.7.0</version>
                </dependency>
            </dependencies>
        </plugin>

重要:<pluginManagment>タグの下にタグを使用しないで<build>ください。pom.xml は次のようにする必要があります。

<project ....>
    ....
    <dependencies>
        <dependency> 
                    ....
        </dependency>
                ....
    </dependencies>
    <build>
        <plugins>
            <plugin>
                            ....
            </plugin>
                        ....
        </plugins>
    </build>
</project>

最後に<context:spring-configured />、Spring アプリケーションのコンテキスト構成ファイルに追加します。

POJO クラスにアノテーションを付け、アノテーション@Configurableを使用して Spring Bean を注入できるようになりました@Autowired。この方法では、その POJO の新しいインスタンスを作成するたびに、自動的に構成されます (たとえば、依存関係が注入されます)。

于 2014-03-07T23:09:03.650 に答える