spring aop 如何切面到mvc 的controller, service

[size=large][color=red]Spring+SpringMVC+Mybatis 利用AOP自定义注解实现可配置日志快照记录[/color][/size] [url]http://unkeltao.com/blog/2014/07/22/spring-plus-springmvc-plus-mybatis-aop/[/url]
[size=large][color=red]基于注解的Spring AOP的配置和使用[/color][/size] [url]http://my.oschina.net/sniperLi/blog/491854[/url]
Spring中的AOP(五)——在Advice方法中获取目标方法的参数 [url]http://my.oschina.net/itblog/blog/211693[/url], [color=red]这里有aop的更多知识[/color]


[size=large][color=red]拦截Controller[/color][/size]
[url]http://yjian84.iteye.com/blog/1920787[/url]

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.


这个人说的好像很对啊。[color=red]我把aspectj 和springmvc的配置文件放到一起就可以用到controller上了[/color]。

在servlet.xml加入
<aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>



sysLogAspectJ
package com.pandy.core.aop;

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;

/**
* 项目名称: wp_idea_linux
* 功能说明: 在servlet.xml配置: <aop:aspectj-autoproxy proxy-target-class="true" />
* 创建者: Pandy,
* 邮箱: panyongzheng@163.com, 1453261799@qq.com
* 版权:
* 官网:
* 创建日期: 15-11-13.
* 创建时间: 下午9:42.
* 修改历史:
* -----------------------------------------------
*/
@Aspect
@Component
public class ControllerLogAspect {

@Pointcut("within(@org.springframework.stereotype.Controller *)")
public void cutController(){
}

@Around("cutController()")
public Object recordSysLog(ProceedingJoinPoint point) throws Throwable{
System.out.println("=================================ControllerLogAspect执行方法2");
return point.proceed();
}
}




拦截Service等
在applicationContext.xml(扫描service的类的配置文件)加入
<aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>



package com.pandy.core.aop;

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;

/**
* 项目名称: wp_idea_linux
* 功能说明: 在applicationContext.xml(扫描service的配置文件)配置: <aop:aspectj-autoproxy proxy-target-class="true" />
* 创建者: Pandy,
* 邮箱: panyongzheng@163.com, 1453261799@qq.com
* 版权:
* 官网:
* 创建日期: 15-11-13.
* 创建时间: 下午9:42.
* 修改历史:
* -----------------------------------------------
*/
@Aspect
@Component
public class ServiceLogAspect {

@Pointcut("within(@org.springframework.stereotype.Service *)")
public void cutService(){
}

@Around("cutService()")
public Object recordSysLog(ProceedingJoinPoint point) throws Throwable{
System.out.println("=================================ServiceLogAspect执行方法2");
return point.proceed();
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值