标签:inf exec col ssi aspectj spring 目标 date() auto
<?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>
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(){ } }
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
标签:inf exec col ssi aspectj spring 目标 date() auto
原文地址:https://www.cnblogs.com/windbag7/p/9566531.html