2014-01-25 24 views
0

我定義了一個看點及其在應用程序的context.xml這樣的配置:切入點

<aop:aspectj-autoproxy proxy-target-class="true"/> 
<bean id="myAspect" class="myAspectClass"/> 
<aop:config> 
    <aop:aspect ref="myAspect"> 
     <aop:pointcut id="myPointCut" expression="(@within(MyPointCutAnnotation) or @annotation(MyPointCutAnnotation)) and execution(* *(..))"/> 
     <aop:before pointcut-ref="myPointCut" method="beforeMethod" /> 
    </aop:aspect> 
</aop:config> 

而且我的servlet-context.xml中看起來是這樣的:

<context:annotation-config /> 
    <context:component-scan base-package="mypackage" /> 

我的春天Controller類:

@Controller 
@RequestMapping(value="/xxx", method=RequestMethod.POST, consumes=MediaType.APPLICATION_JSON_VALUE) 
public class MyController { 
    @MyPointCutAnnotation  
    @ResponseBody 
    @RequestMapping(value="/signOn") 
    public void myMethod() { 
     ...do something 
    } 
} 

的web.xml文件是如下:

<context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>classpath:application-context.xml</param-value> 
    </context-param> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
    <servlet> 
     <servlet-name>appServlet</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>classpath:servlet-context.xml</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

但myPointCut不會被觸發myMethod的,除非我的servlet-context.xml中添加以下(進口根上下文)是這樣的:

<import resource="classpath:application-context.xml"/ > 

有人能告訴我這是爲什麼發生的? Spring中通過上下文層次結構不是包含Aspect,PointCut和Advice在內的所有bean都自動提供給Controller嗎?

+0

您使用的是maven嗎?如果是的話,你可以分享你的pom.xml的構建標籤部分?可能你需要添加aspectj builder你的pom.xml。 – nsylmz

+0

是的,我使用Maven,這裏是我的體型標籤部分:' \t \t \t \t \t < - 使用的Java 1.6,而不是默認的1.5的源/目標爲 \t \t \t \t javac的!編譯器 - > \t \t \t \t \t \t \t 行家編譯-插件 \t \t \t \t <結構> \t \t \t \t \t $ {javase.version} \t \t \t \t \t $ {javase.version} \t \t \t \t \t UTF-8 \t \t \t \t \t \t \t \t \t \t' – jl276

+0

我不熟悉AspectJ的建設者。它是什麼以及它如何幫助解決問題? – jl276

回答

0

將此代碼添加到您的網站。可能是你的問題可以解決。

<properties> 
    <aspectj.version>1.7.4</aspectj.version> 
    <java.version>1.7</java.version> 
</properties> 

<build> 
    <plugins> 
     <plugin> 
      <groupId>org.codehaus.mojo</groupId> 
      <artifactId>aspectj-maven-plugin</artifactId> 
      <version>1.5</version> 
      <dependencies> 
       <dependency> 
        <groupId>org.aspectj</groupId> 
        <artifactId>aspectjtools</artifactId> 
        <version>${aspectj.version}</version> 
       </dependency> 
      </dependencies> 
      <executions> 
       <execution> 
        <goals> 
         <goal>compile</goal> 
         <goal>test-compile</goal> 
        </goals> 
       </execution> 
      </executions> 
      <configuration> 
       <outxml>true</outxml> 
       <complianceLevel>${java.version}</complianceLevel> 
       <aspectLibraries> 
        <aspectLibrary> 
         <groupId>org.springframework</groupId> 
         <artifactId>spring-aspects</artifactId> 
        </aspectLibrary> 
       </aspectLibraries> 
       <source>${java.version}</source> 
       <target>${java.version}</target> 
      </configuration> 
     </plugin> 
    </plugins> 
</build>