AspectJ简介
- ·AspectJ是一个基于Java语言的AOP框架
- ·Spring2.0以后新增了对AspectJ切点表达式支持
- ·@AspectJ是AspectJ1.5新增功能,通过JDK5注解技术,允许直接在Bean类中定义切面
- ·新版本Spring框架,建议使用AspectJ方式来开发AOP
- ·使用AspectJ需要导入Spring AOP和 Aspect/相关jar包
- -spring-aop-4.2.4.RELEASE.jar
- -com.springsource.org.aopalliance-1.0.0.jar
- -spring-aspects-4.2.4.RELEASE.jar
- -com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
基于AspectJ的注解AOP开发
注解开发:环境准备
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" 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 definitions here --> <!--开启AspectJ的注解开发,自动代理=====================--> <aop:aspectj-autoproxy/> </beans>
@AspectJ提供不同的通知类型
- ·@Before 前置通知,相当于BeforeAdvice
- ·@AfterReturning 后置通知,相当于AfterReturningAdvice
- ·@Around 环绕通知,相当于MethodInterceptor
- ·@AfterThrowing异常抛出通知,相当于ThrowAdvice
- ·@After最终final通知,不管是否异常,该通知都会执行
- ·@DeclareParents 引介通知,相当于IntroductionInterceptor(不要求掌握)
在通知中通过value属性定义切点
- ·通过execution函数,可以定义切点的方法切入
- ·语法:
- -execution(<访问修饰符>?<返回类型><方法名>(<参数>)<异常>)
- ·例如
- -匹配所有类public方法 execution(public**(.))
- -匹配指定包下所有类方法 execution(*com.imooc.dao.*(..))不包含子包
- -execution(*com.imooc.dao..*(..))..*表示包、子孙包下所有类
- -匹配指定类所有方法 execution(*com.imooc.service.UserService.*(..))
- -匹配实现特定接口所有类方法
- execution(*com.imooc.dao.GenericDAO+.*(..))
- -匹配所有save开头的方法 execution(*save*(..))
案例(前置通知)
MyAspectAnno
@Aspect public class MyAspectAnno { @Before(value = "execution(* com.windbag.aspectJ.demo1.ProductDao.save(..))") public void before(){ System.out.println("前置通知====================="); } }
ProductDao
public class ProductDao { public void save(){ System.out.println("保存商品"); } public void update(){ System.out.println("更改商品"); } public void findone(){ System.out.println("查询一个商品"); } public void delete(){ System.out.println("删除商品"); } public void findAll(){ System.out.println("查询所有商品"); } }
SpringDemo1Test
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class SpringDemo1Test { @Resource(name="productDao") private ProductDao productDao; @Test public void demo1(){ productDao.delete(); productDao.findAll(); productDao.findone(); productDao.save(); productDao.update(); } }
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" 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 definitions here --> <!--开启AspectJ的注解开发,自动代理=====================--> <aop:aspectj-autoproxy/> <!--目标类==============================================--> <bean id="productDao" class="com.windbag.aspectJ.demo1.ProductDao"/> <!--定义切面--> <bean class="com.windbag.aspectJ.demo1.MyAspectAnno"/> </beans>
其余的通知类型
@Aspect public class MyAspectAnno { @Before(value = "myPointcut1()") public void before(){ System.out.println("前置通知=================="); } @AfterReturning(value = "execution(* com.windbag.aspectJ.demo1.ProductDao.update(..))",returning = "result") public void afterReturing(Object result){ System.out.println("后置通知==================" + result); } @Around(value = "execution(* com.windbag.aspectJ.demo1.ProductDao.delete(..))") public Object around(ProceedingJoinPoint joinPoint) throws Throwable { System.out.println("环绕前通知================"); /*执行目标方法*/ Object obj = joinPoint.proceed(); System.out.println("环绕后通知================"); return obj; } @AfterThrowing(value = "execution(* com.windbag.aspectJ.demo1.ProductDao.findone(..))",throwing = "e") public void afterThrowing(Throwable e){ System.out.println("异常抛出通知=============" + e.getMessage()); } @After(value = "execution(* com.windbag.aspectJ.demo1.ProductDao.findAll(..))") public void after(){ System.out.println("最终通知================"); } @Pointcut(value = "execution(* com.windbag.aspectJ.demo1.ProductDao.save(..))") private void myPointcut1(){ } }
基于AspectJ的XML方式的AOP开发
案例
CustomerDao
public interface CustomerDao { public void save(); public String update(); public void delete(); public void findOne(); public void findAll(); }
CustomerImpl
public class CustomerDaoImpl implements CustomerDao { @Override public void save() { System.out.println("保存客户"); } @Override public String update() { System.out.println("修改客户"); return "hello"; } @Override public void delete() { System.out.println("删除客户"); } @Override public void findOne() { System.out.println("查询一个客户"); //int a = 1/0; } @Override public void findAll() { System.out.println("查询多个客户"); } }
SpringDemo2Test
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext2.xml") public class SpringDemo2Test { @Resource(name="customerDao") private CustomerDao customerDao; @Test public void demo1(){ customerDao.save(); customerDao.delete(); customerDao.findAll(); customerDao.findOne(); customerDao.update(); } }
MyAspectXml
public class MyAspectXml { //前置通知 public void before(JoinPoint joinPoint){ System.out.println("XML方式的前置通知" + joinPoint); } //后置通知 public void afterReturing(Object result){ System.out.println("XML方式的后置通知" + result); } //环绕通知 public Object around(ProceedingJoinPoint joinPoint) throws Throwable{ System.out.println("XML方式的环绕前通知"); Object obj = joinPoint.proceed(); System.out.println("XML方式的环绕后通知"); return obj; } //异常抛出通知 public void afterThrowing(Throwable e){ System.out.println("XML方式的异常抛出通知" + e.getMessage()); } //最终通知 public void after(){ System.out.println("XML方式的最终通知"); } }
applicationContext2.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" 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 definitions here --> <!--XML的配置方式完成AOP的开发--> <!--配置目标类================--> <bean id="customerDao" class="com.windbag.aspectJ.demo2.CustomerDaoImpl"/> <!--配置切面类--> <bean id="myAspectXml" class="com.windbag.aspectJ.demo2.MyAspectXml"/> <!--aop的相关配置============--> <aop:config> <!--配置切入点--> <aop:pointcut id="pointcut1" expression="execution(* com.windbag.aspectJ.demo2.CustomerDao.save(..))"/> <aop:pointcut id="pointcut2" expression="execution(* com.windbag.aspectJ.demo2.CustomerDao.update(..))"/> <aop:pointcut id="pointcut3" expression="execution(* com.windbag.aspectJ.demo2.CustomerDao.delete(..))"/> <aop:pointcut id="pointcut4" expression="execution(* com.windbag.aspectJ.demo2.CustomerDao.findOne(..))"/> <aop:pointcut id="pointcut5" expression="execution(* com.windbag.aspectJ.demo2.CustomerDao.findAll(..))"/> <!--配置AOP的切面--> <aop:aspect ref="myAspectXml"> <!--配置前置增强--> <aop:before method="before" pointcut-ref="pointcut1"/> <!--配置后置增强--> <aop:after-returning method="afterReturing" pointcut-ref="pointcut2" returning="result"/> <!--配置环绕增强--> <aop:around method="around" pointcut-ref="pointcut3"/> <!--配置异常抛出--> <aop:after-throwing method="afterThrowing" pointcut-ref="pointcut4" throwing="e"/> <!--配置增强--> <aop:after method="after" pointcut-ref="pointcut5"/> </aop:aspect> </aop:config> </beans>
项目源码
https://github.com/ryanxu47/spring_aspect
原文地址:https://www.cnblogs.com/windbag7/p/9566531.html
时间: 2024-11-11 19:06:55