• spring aop 如何切面到mvc 的controller--转载


    原文:http://yjian84.iteye.com/blog/1920787

    网上搜罗半天,不知道什么原因,看了源码,好像他们说的controller 是不受代理的,也对哈,不知道怎么办,于是在http://stackoverflow.com/questions/17834958/spring-aop-is-not-working-in-with-mvc-structure?rq=1 这个地方有个人说了: 

    <context:component-scan base-package="com.dao" /> 
    <mvc:annotation-driven/> 

    <aop:aspectj-autoproxy />   
    from application-context.xml to controller-servlet.xml? 

    The aspects and the beans to be applied needs to be in the same ApplicationContext but ApplicationContext is not aware of WebApplicationContext . 


    Indeed your controller (annotated by @Controller) and your aspects (annotated by @Aspect) should be in the same Spring context. 

    Usually people define their controllers in the dispatch-servlet.xml or xxx-servlet.xml and their service beans (including the aspects) in the main applicationContext.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的配置文件放到一起就可以用到controller上了。 

    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();
        }
        
        
        
    }
  • 相关阅读:
    程序员面试金典--最大和子矩阵
    hihocoder-[Offer收割]编程练习赛25
    牛客网--今日头条2017后端工程师实习生笔试题
    牛客网-网易2017内推笔试编程题合集(二)
    hihocoder-1565-大富翁
    引用传递和值传递
    SSH详解
    Oracle中的游标
    Oracle触发器-变异表触发器不能访问本表
    Oracle中 in、exists、not in,not exists的比较
  • 原文地址:https://www.cnblogs.com/davidwang456/p/4121765.html
Copyright © 2020-2023  润新知