2009-05-23 45 views
25

更新5:我已經下載了基於最新Eclipse的最新Spring ToolsSuite IDE。當我將項目導入爲Maven項目時,Eclipse/STS似乎使用Maven目標來構建我的項目。這意味着AspectJ最終能夠在Eclipse中正確工作。爲什麼AspectJ不能編譯Spring的@Configurable工作?

更新4:我已經結束了使用Maven + AspectJ插件編譯時編織,有效地繞過了Eclipse的機制。

更新3:看來AspectJ的Eclipse插件破壞了Eclipse正確發佈到Tomcat的能力。只有通過刪除項目上的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> 

實際上,我擔心我的this problem,這裏一切正常不一致重演。隨着我瞭解更多,我會繼續更新這個問題。

關於Eclipse,我通過採用我希望編織的二進制方面取得了一些進展 - 在此例中爲spring-aspects.jar - 並將其從我的類路徑中複製出來。然後我添加這個現在的外部罐子到我的方面路徑。做完這些之後,Eclipse在我的代碼中正確顯示了我的AspectJ標記。令人煩惱的是,我不能僅僅通過Maven插件爲我保留spring-aspects.jar我的Java構建路徑。但是,由於某些原因,AspectJ插件沒有看到二進制方面的內容,除非它們明確添加到方面路徑


原貼: @Configurable是一個Spring註釋允許依賴關係被注入實例化外部彈簧的對象(例如,通過休眠或一些工廠類)。

我之前在加載時編織時使用了這個註釋,它主要是,其中大部分是。偶爾我會開機,沒有任何東西會被注入。此問題產生了this StackOverflow question。沒有太多的答案,但大多數人建議我嘗試編譯時編織,而不是由於更高的可靠性。

我爲Eclipse和Maven安裝了AspectJ插件。這兩個都會產生看起來是正確編譯的類。在AspectJ編譯之前,我已經在文本編輯器中打開了其中的一個類,並且未找到對AspectJ的引用。我在AspectJ編譯後打開它,Eclipse和Maven生成版本都提供了對org.aspectj.weaver.MethodDeclarationLineNumber的引用。這就是爲什麼我認爲它被正確編譯。問題是,一旦部署,就不會注入依賴關係。

我的春天的applicationContext.xml不包括以下內容:

<context:spring-configured /> 

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

是上述所有需要的類標記@Configurable已經DI做了什麼?在從加載時編織編譯時編織的轉換,我刪除META-INF/aop.xml文件<背景下:加載時,韋弗/從我applicationContext.xml中和Spring的Tomcat韋弗>從我的context.xml

如何進一步調查此問題?什麼是可能的原因?

回答

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> 

它做作爲兩個獨立的執行步驟,以使您可以添加不同的方面庫進行單元測試和編譯。

您還需要添加彈簧方面庫中的以下依賴性:

<dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-aspects</artifactId> 
     <scope>compile</scope> 
    </dependency> 
+3

您需要爲aspectj-maven-plugin配置添加 1.6,否則根據錯誤消息以錯誤(configure.incompatibleComplianceForSource)結束,可能是因爲complianceLevel默認爲1.4,它與源設置不同在你的情況下,這是1.6的 – 2013-10-09 14:20:49

7

如果這是您的替代方案,我在我的應用程序中成功配置了加載時織入。

我的環境:

  • JDK-1.6
  • 彈簧2.5.6
  • JPA用的EclipseLink-1.1.0

配置詳情:

Spring 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> 

Spring註解

@Configurable("baseEntity") 
public abstract class BaseEntity 

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

Java虛擬機參數

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

historyHandler和baseEntitty的實例由ecliselink創建。 historyEvent中的historyHandler和historyHandler中的historyDao通過加載時間記錄進行設置。

您可以在Eclipse運行配置或Tomcat catalina.sh/bat中設置VM參數。

2

至於你的Eclipse的classpath的問題而言,你可能會發現這很有用。

m2eclipse plugin有一個可選AJDT integration。該集成將讀取aspectj-maven-plugin配置的aspectLibraries部分,並將jar添加到Eclipse的Aspect Path中。

4

創建@configurable類的字段如果您未針對此批註正確配置彈簧,Autowired會拋出NullPointerException。 請按照下列步驟進行@Configurable註解正確

工作,這種方法被稱爲AspectJ的建造時間織注入春天豆到非彈簧製造類

第一步是在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應用程序上下文配置文件。

現在,您可以使用@Autowired註釋將POJO類標註爲@Configurable,並在其中注入彈簧豆。通過這種方式,無論您何時創建該POJO的新實例,它都將自動進行配置(例如注入依賴關係)。