利用Spring AOP机制拦截方法一例
2016-09-23 17:04
246 查看
直接上代码:
拦截方法:com.xxxx.wx.mapper.MymessageMapper.insert(Mymessage message)
插入消息记录之后,马上推送消息。
配置:
spring配置文件中加入扫描:
<context:component-scan base-package="com.xxxx.wx.service, com.xxxx.wx.aop" />
com.xxxx.wx.aop 这个是MessageInterceptor 所在的包。
So easy, and so powerfull.
注:还有一个问题没有考虑,如果插入数据到数据中时失败了,拦截器会怎么样呢?是否会推送消息呢?后面再来考虑和完善。
@Aspect // for aop @Component // for auto scan @Order(0) // execute before @Transactional public class MessageInterceptor { @Autowired private UserService userService; private static Logger logger = LoggerFactory.getLogger(MessageInterceptor.class); @Pointcut("execution(public * com.xxx.wx.mapper.MymessageMapper.insert(..))") public void messageInsertAspect(){ } //@Before("messageInsertAspect()") @After("messageInsertAspect()") public void messageInsert(JoinPoint joinPoint) { if(joinPoint.getArgs() != null){ Mymessage message = (Mymessage) joinPoint.getArgs()[0]; logger.debug(JSON.toJSONString(message)); // ..... try { sendMyMessageNotice(token, message); // 发送审核通过消息 } catch (IOException e) { e.printStackTrace(); logger.debug(e.toString()); } } }
拦截方法:com.xxxx.wx.mapper.MymessageMapper.insert(Mymessage message)
插入消息记录之后,马上推送消息。
配置:
spring配置文件中加入扫描:
<context:component-scan base-package="com.xxxx.wx.service, com.xxxx.wx.aop" />
com.xxxx.wx.aop 这个是MessageInterceptor 所在的包。
So easy, and so powerfull.
注:还有一个问题没有考虑,如果插入数据到数据中时失败了,拦截器会怎么样呢?是否会推送消息呢?后面再来考虑和完善。
相关文章推荐
- 利用Spring AOP 缓存方法结果集
- 利用Spring AOP 缓存方法结果集(ehcache)
- spring的aop拦截action中的方法会报java.lang.NoSuchMethodException: $Proxy97.test()异常
- 利用Spring AOP记录方法的执行时间
- Spring AOP不拦截从对象内部调用的方法原因
- 利用Spring AOP 缓存方法结果集
- 【Spring开发】―― AOP之方法级拦截
- Spring AOP 各种拦截方法执行的顺序
- Spring3系列10- Spring AOP——Pointcut,Advisor拦截指定方法
- spring Aop拦截controller方法
- 基于 Annotation 拦截的 Spring AOP 权限验证方法
- 【Spring开发】—— AOP之方法级拦截
- Spring AOP代理机制以及方法调用在同一个类的另一个方法的问题
- 利用spring AOP 和注解实现方法中查cache-我们到底能走多远系列(46)
- spring aop 同一个类中一个方法调用另一个方法,拦截不到被调用的方法
- 基于 Annotation 拦截的 Spring AOP 权限验证方法
- Spring AOP Maven依赖,拦截加注解的方法参数
- 利用.NET Remoting基础架构中的真实代理/透明代理技术实现了不针对具体类型、具体方法的通用方法调用拦截机制
- Spring3系列10-Spring AOP——Pointcut,Advisor拦截指定方法
- Spring AOP不拦截从对象内部调用的方法原因