spring aopはどのようにmvcのcontrollerまでうどんを切りますか?

5189 ワード

原文:http://yjian84.iteye.com/blog/1920787
ネットで半日探しましたが、原因が分かりません。ソースを見たら、彼らが言っているようです。controllerは代理を受けないので、正しいです。どうすればいいか分かりません。http://stackoverflow.com/questions/17834958/spring-aop-is-not-working-in-with-mvc-structure?rq=1 ここで誰かが言いました。      from appration-context.xml to controller-servlet.xml? The aspects and the beans to be aplied needs to be in the same Apple Contront but Apple but Application Comptext is not aware of WebApple Contect. Indeed your controller(annotated by@Controller)and your aspects(annotated by@Asppect)shuld be in the same Spring context. Usually people define their controllers in the dispatch-servlet.xml or xx-servlet.xml and their service beans(including the aspects)in the mail appication Cont.xt.xml.It will not work. When Spring initializes the MVC context,it will create a proxy for your controller but if your aspects are not in the same context,Spring will not create interceptors for them. この人の話は正しいようです。私はaspectjとspringmvcの配置ファイルを一緒に置くとcontrolerに使えるようになります。 spring-mvc.xml 
    <context:component-scan base-package="com.cms.controller" />
    <context:component-scan base-package="com.cms.aspectj" />
    <!-- <bean id="sysLogAspectJ" class="com.cms.aspectj.SysLogAspectJ" /> -->
    <aop:aspectj-autoproxy proxy-target-class="true">
    <!--     <aop:include name="sysLogAspectJ" /> -->
    </aop:aspectj-autoproxy>
    <!-- <mvc:annotation-driven /> -->
    <bean
        class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
    <bean
        class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" />
package com.cms.aspectj;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class SysLogAspectJ {

    @Pointcut("within(@org.springframework.stereotype.Controller *)")
    public void cutController(){
        
    }
    
    @Around("cutController()")
    public Object recordSysLog(ProceedingJoinPoint point) throws Throwable{
        System.out.println("lfkdjj================================================================");
        return point.proceed();
    }
    
    
    
}