一、AOP 简介:百度百科;
面向切面编程(也叫面向方面编程):Aspect Oriented Programming(AOP),是软件开发中的一个热点,也是 Spring框架中的一个重要内容。利用 AOP 可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
主要的功能是:日志记录,性能统计,安全控制,事务处理,异常处理等等。
二、开发步骤
1、model、service、serviceimpl、相继建立
2、切面类
public class StudentServiceAspect {
private Logger logger = Logger.getLogger(StudentServiceAspect.class);
public void doBefore(JoinPoint jp) {
logger.info("测试 开始-----------------------------");
logger.info(jp.getTarget().getClass().getName()+":"+jp.getSignature().getName()+":"+Arrays.toString(jp.getArgs()));
}
public void doAfter(JoinPoint jp) {
logger.info("测试完毕--------------------------------------------");
}
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
logger.info("环绕开始执行------------------------");
// 方法名和返回值
logger.info(pjp.getSignature().getName());
Object obj = pjp.proceed();
logger.info("返回值:"+obj);
logger.info("环绕执行完毕---------------------------");
return obj;
}
public void doReturn(JoinPoint jp) {
logger.info("返回值是1--------------------------------------------");
}
public void doExcep(JoinPoint jp,Throwable ex) {
logger.info("程序出异常了--------------------------------------------");
logger.info(ex.getMessage());
}
}
3、sericeimpl中的
public class StudentServiceImpl implements StudentService{
@Override
public void addStudent(Student stu) {
System.out.println("添加学生信息"+stu.getName()+"---------------");
}
@Override
public int updateStudent(Student stu) {
System.out.println("更新学生信息"+stu.getName()+"----------------");
return 1;
}
}
4、resources中的配置
bean中的配置
<bean id="aspect" class="com.mao.spring.aop.aspect.StudentServiceAspect" />
<bean id="student" class="com.mao.spring.aop.model.Student">
<property name="id" value="11" />
<property name="name" value="张刚" />
</bean>
<bean id="studentService" class="com.mao.spring.aop.service.impl.StudentServiceImpl"></bean>
切面配置
<aop:config>
<aop:aspect id="stuAspect" ref="aspect">
<aop:pointcut expression="execution(* com.mao.spring.aop.service..*.*(..))" id="aa"/>
<aop:before method="doBefore" pointcut-ref="aa"/>
<aop:after method="doAfter" pointcut-ref="aa"/>
<aop:around method="doAround" pointcut-ref="aa"/>
<aop:after-returning method="doReturn" pointcut-ref="aa"/>
<aop:after-throwing method="doExcep" pointcut-ref="aa" throwing="ex"/>
</aop:aspect>
</aop:config>
5、测试
public class StudentServiceTest {
private Logger logger=Logger.getLogger(StudentServiceTest.class);
private ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContent.xml");
@Test
public void testAdd() {
Student stu = ac.getBean("student", Student.class);
StudentService studentService = ac.getBean("studentService",StudentService.class);
studentService.addStudent(stu);
logger.info(" -----------------添加结束 -----------------------------");
}
@Test
public void testUpdate() {
Student stu = ac.getBean("student", Student.class);
StudentService studentService = ac.getBean("studentService",StudentService.class);
studentService.updateStudent(stu);
logger.info(" -----------------更新结束 -----------------------------");
}
}
测试结果
[main] INFO com.mao.spring.aop.aspect.StudentServiceAspect - 测试 开始-----------------------------
[main] INFO com.mao.spring.aop.aspect.StudentServiceAspect - com.mao.spring.aop.service.impl.StudentServiceImpl:addStudent:[Student [id=11, name=张刚]]
[main] INFO com.mao.spring.aop.aspect.StudentServiceAspect - 环绕开始执行------------------------
[main] INFO com.mao.spring.aop.aspect.StudentServiceAspect - addStudent
添加学生信息张刚---------------
[main] INFO com.mao.spring.aop.aspect.StudentServiceAspect - 返回值是1--------------------------------------------
[main] INFO com.mao.spring.aop.aspect.StudentServiceAspect - 返回值:null
[main] INFO com.mao.spring.aop.aspect.StudentServiceAspect - 环绕执行完毕---------------------------
[main] INFO com.mao.spring.aop.aspect.StudentServiceAspect - 测试完毕--------------------------------------------
[main] INFO com.mao.spring.aop.service.StudentServiceTest - -----------------添加结束 -----------------------------