使用Aspectj进行AOP开发
来源:互联网 发布:js分割数字 编辑:程序博客网 时间:2024/05/21 14:55
添加类库:aspectjrt.jar和aspectjweaver.jar
添加aop schema.
定义xml元素:<aop:aspectj-autoproxy>
编写java类,并用@Aspect注解成通知
AspectJ 支持 5 种类型的通知注解:
@Before: 前置通知, 在方法执行之前执行
@After: 后置通知, 在方法执行之后执行
@AfterReturning: 返回通知, 在方法返回结果之后执行
@AfterThrowing: 异常通知, 在方法抛出异常之后
@Around: 环绕通知, 围绕着方法执行
配置成普通bean元素即可.
二、
后置通知:@After
@After("execution(* *..WelcomeService.*(..))")
public void applaud(){..}
后置通知在目标方法执行完成之后执行.一个切面aspect包含很多通知.
@After
后置通知表明目标方法执行完之后,不论是否抛异常,都会织入该通知.
@AfterReturning
方法返回后通知只在目标方法返回以后执行,若抛异常不执行.
@AfterReturning(pointcut="",returning="res")
public void xxx(Joinput jp,Object res)
在AfterReturning
通知中可接收到返回值.res即是用来接收返回值的对象.
三、
环绕通知:@Around
@Around("execution(* *..WelcomeService.*(..))")
public void around(ProceedingPointCut jp){..}
注意:可以控制目标方法是否调用,以及返回完全不同的对象,要慎用.
指定优先级:
@Aspect
@Order(0)
public class xxx{...}
加上@Order注解可以指定加入切面的优先级(先后顺序,值越小,优先级越高)
例子说明:
AdviceImpl类:
package www.csdn.spring.advice.aspetjs;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;@Aspectpublic class AdviceImpl {@Before(value = "execution(* UserDaoImpl.*(..))")public void doTransAction(){System.out.println("----开启事务-----");}@After(value = "execution(* www.csdn..UserDaoImpl.*(..))")public void doAfterTransAction(){System.out.println("-------提交事务-------");}@Around(value = "execution(* www.csdn..UserDaoImpl.save(..))")public void doSec(ProceedingJoinPoint jp){System.out.println("----------安全处理之前-------------");try {Object obj = jp.proceed();} catch (Throwable e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println("----------安全处理之后-------------");}}
配置文件:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd "> <!-- 通知 --> <bean id="adviceImpl" class="www.csdn.spring.advice.aspetjs.AdviceImpl"/> <!-- 配置bean --> <bean id="userDaoImpl" class="www.csdn.spring.advice.aspetjs.UserDaoImpl"/> <aop:aspectj-autoproxy/></beans>
测试类:
package www.csdn.spring.advice.aspetjs;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class AspetjsTest {@Testpublic void test(){ApplicationContext context=new ClassPathXmlApplicationContext("spring-asp*.xml");UserDao uDao=(UserDao) context.getBean("userDaoImpl");uDao.save(null);/*uDao.delete(null);try{uDao.update(null);}catch(Exception e){}uDao.getObjects();*/}}
例子说明:在配置文件中出现的UserDaoImpl可以自己在相应的包中自己建,最好是新建UserServiceImpl为好。
- 使用Aspectj进行AOP开发
- 使用Aspectj进行AOP开发
- 使用Aspectj进行AOP开发
- spring入门(10)---使用Aspectj进行AOP开发
- Spring使用AspectJ进行AOP的开发:注解方式
- Spring -- aop, 用Aspectj进行AOP开发
- spring 学习笔记 使用aspectj开发aop
- spring心得10--使用Aspectj进行AOP开发介绍及案例分析
- 基于AspectJ的XML方式进行AOP开发
- 基于AspectJ的注解方式进行AOP开发
- AOP用AspectJ开发
- 基于@Aspectj使用Spring aop @Around进行权限拦截
- Spring-AOP 使用@AspectJ
- Spring AOP基于@AspectJ开发
- Spring AOP使用ASPECTJ错误
- Spring aop之使用AspectJ
- AOP之使用AspectJ案例
- 使用Spring(十)使用Spring进行面向切面编程(AOP)@AspectJ支持
- 系统吞吐量(TPS)、用户并发量、性能测试概念和公式
- 计算机组成实验-第7章_单时钟数据通路设计
- poj 3254 Corn Fields (状态压缩dp)
- (18)集合&map
- hdu 4217 线段树
- 使用Aspectj进行AOP开发
- Linux自启动设置详解
- (19)集合工具类
- 【Learning】Distributed System - Serialization
- Pythont特殊语法filter,map,reduce,apply
- (20)Io流字符流
- 广告费与业务宣传费的区别
- 【Learning】Java Date and Time
- 浅谈Struts2拦截器的原理与实现