spring2.5学习笔记(七):AOP基本概念和注解方式实现AOP
AOP基本概念学习:
Aspect(切面):指横切性关注点的抽象即为切面,它与类相似,只是两者的关注点不一样,类是对事物特征的抽象,而切面是横切性关注点的抽象。
joinpoint(连接点):所谓连接点是指那些被拦截到的点。在spring中,这些点指的是方法,而且spring只支持方法类型的连接点,实际上joinpoint还可以是field或者类构造器
pointcut(切入点):所谓切入点是指我们要对那些joinpoint进行拦截的定义
advice(通知):所谓的通知是指拦截到joinpoint之后所要做的事情。分为前置通知,后置通知,异常通知,最终通知,环绕通知
Target(目标对象):被代理的对象
Wcave(织入):指将aspects应用到target对象并导致proxy对象创建的过程
Introduction(引入):在不修改类代码的前提下,Introduction可以在运行期为类动态地增加一些方法或者field
代码一:接口以及实现类,
package cn.itcast.server;public interface IPersonService {public void save(String name);public String getPersonName(Integer id);public void update(String name,Integer id);}
package cn.itcast.server.impl;import cn.itcast.server.IPersonService;public class PersonServiceBean implements IPersonService {public String getPersonName(Integer id) {System.out.println("这是getPersonName方法");return "xxx";}public void save(String name) {//throw new RuntimeException("我是例外");System.out.println("这是save方法");}public void update(String name, Integer id) {System.out.println("这是update方法");}}
package cn.itcast.server;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.AfterThrowing;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;/** * 定义切面 */@Aspect public class MyInteceptor {//定义切入点,换个说法就是定义要被拦截的方法@Pointcut("execution(* cn.itcast.server.impl.PersonServiceBean.*(..) )")private void anyMethod(){}//定义切入点的名称//定义前置通知@Before("anyMethod() && args(name)")//这里的args(name)是说明需要拦截的方法必须是带有一个参数的方法public void doAccessCheck(String name){System.out.println("这里输出的是前置通知 " + name);}//定义后置通知@AfterReturning(pointcut="anyMethod()",returning="res")//这里的returning 是接收的被拦截方法的返回值public void doAfterReturning( String res){System.out.println("这里输出的是后置通知 " + res);}//定义最终通知@After("anyMethod()")public void doAfterReturn(){System.out.println("这里输出的是最终通知");}//定义例外通知@AfterThrowing(pointcut="anyMethod()",throwing="e")public void doAfterThrowing(Exception e){System.out.println("这里输出的是例外通知 "+ e);}//定义环绕通知@Around("anyMethod()")public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable{//判断用户是否具有权限System.out.println("进入方法");Object result = pjp.proceed();System.out.println("退出方法");return result;}}
<?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd "><!--AOP开发(基于注解方式进行AOP开发) --><aop:aspectj-autoproxy/> <bean id="myInteceptor" name="code">package junit.test;import org.junit.BeforeClass;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import cn.itcast.server.IPersonService;public class SpringAOPTest {@BeforeClasspublic static void setUpBeforeClass() throws Exception {}@Test public void interceptorTest(){ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");IPersonService ipersonService = (IPersonService) ac.getBean("personService");ipersonService.update("xxx",2);ipersonService.save("xx");ipersonService.getPersonName(2);}}