AOP 切面编程学习笔记
介绍: aop.aspect这个包里面放的是切面类,这是一个关注点(共同业务)的模块化,这个关注点可能会横切多个对象
aop.service这个包里面放的是被切面横切的目标,EmpService Deptservice
aop-beans是配置文件,里面有bean的配置,切入点(切入点里面有一个表达式,可以判断哪些方法需要切入)的配置,通知(判断切入到哪里,执行什么方法)等。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package aop.aspect; 2 3 public class Myaspect { 4 public void afterReturn(Object retVal){ 5 if(retVal==null){ 6 retVal=""; 7 } 8 System.out.println("aspect's afterReturn execute"+" "+retVal); 9 } 10 11 public void before(){ 12 System.out.println("aspect's before execute"); 13 } 14 15 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package aop.service; 2 3 public class DeptServiceImpl implements DeptService { 4 5 public boolean save() { 6 // TODO Auto-generated method stub 7 System.out.println("DeptServiceImpl's save execute"); 8 return true; 9 } 10 11 public void update() { 12 // TODO Auto-generated method stub 13 14 } 15 16 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package aop.service; 2 3 public class EmpServiceImpl implements EmpService { 4 5 @Override 6 public void delete() { 7 // TODO Auto-generated method stub 8 System.out.println("EmpServiceImpl's delete execute"); 9 } 10 11 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 7 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd 8 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> 9 <bean id="deptService" class="aop.service.DeptServiceImpl" scope="prototype"></bean> 10 <bean id="empService" class="aop.service.EmpServiceImpl" scope="prototype"></bean> 11 <bean id="myaspect" class="aop.aspect.Myaspect"></bean> 12 <aop:config> 13 <aop:aspect id="aspect" ref="myaspect"> 14 <aop:pointcut id="myPointCut" expression="execution (* aop.service.*.* (..))"></aop:pointcut> 15 <!-- 定义前置通知 --> 16 <aop:before pointcut-ref="myPointCut" method="before"></aop:before> 17 <!-- 定义最终通知 --> 18 <aop:after pointcut-ref="myPointCut" method="before"></aop:after> 19 <aop:after-returning pointcut-ref="myPointCut" method="afterReturn" returning="retVal"></aop:after-returning> 20 </aop:aspect> 21 </aop:config> 22 23 24 </beans>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package test; 2 3 import he.bean.PersonAction; 4 5 import org.junit.Test; 6 import org.springframework.context.ApplicationContext; 7 import org.springframework.context.support.AbstractApplicationContext; 8 import org.springframework.context.support.ClassPathXmlApplicationContext; 9 10 import aop.service.DeptService; 11 import aop.service.DeptServiceImpl; 12 import aop.service.EmpService; 13 14 15 public class TestAll { 16 17 @Test 18 public void testPersonAction(){ 19 AbstractApplicationContext ac = new ClassPathXmlApplicationContext("aop-beans.xml"); 20 DeptService deptService = (DeptService) ac.getBean("deptService"); 21 deptService.save(); 22 //EmpService empService = (EmpService) ac.getBean("empService"); 23 //empService.delete(); 24 } 25 26 }