2c Spring-OP.txt

UP 返回
1.AOP概述
	1)	AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。
	2)	AOP是OOP(面向对象编程)的延续,是函数式编程的一种衍生范型。
	3)	利用AOP可以对业务逻辑的各个部分进行隔离,从而降低耦合度,提高程序的可重用性,提高开发的效率。
	4)	AOP采取横向抽取机制,取代了传统纵向继承体系重复性代码
	5)	经典应用案例:事务管理、性能监视、安全检查、缓存 、日志等
	6)	Spring AOP使用纯Java实现,不需要专门的编译过程和类加载器,在运行期通过代理方式向目标类织入增强代码
	7)	AspectJ是一个基于Java语言的AOP框架,Spring2.0开始,Spring AOP引入对Aspect的支持,AspectJ扩展了Java语言,提供了一个专门的编译器,在编译时提供横向代码的织入

   实现原理
	aop底层将采用代理机制进行实现。
	接口 + 实现类 :spring采用 jdk 的动态代理Proxy。
	实现类:spring 采用 cglib字节码增强框架

   AOP术语
	target:目标类,需要被代理的类。例如:UserService
	Joinpoint(连接点):所谓连接点是指那些可能被拦截到的方法。例如:所有的方法
	PointCut 切入点:已经被增强的连接点。例如:addUser()
	advice 通知/增强,增强代码。例如:after、before
	Weaving(织入):是指把增强advice应用到目标对象target来创建新的代理对象proxy的过程.(即代理要干的事,就是将增强代码应用到方法中去)
	proxy 代理类
	Aspect(切面): 切入点pointcut和通知advice结合以后就是一个切面

2.通过jdk代理实现AOP代码
	//接口
	public interface UserService {
		//下面的方法是需要拦截的方法
	    public void addUser(User user);
	    public void updateUser(User user);
	    public int deleteUser(User user);
	}

	//接口实现类
	public class UserServiceImpl implements UserService {
	
	    @Override
	    public void addUser(User user) {
	        System.out.println("添加用户...");
	    }
	
	    @Override
	    public void updateUser(User user) {
	        System.out.println("更新用户...");
	
	    }
	
	    @Override
	    public int deleteUser(User user) {
	        System.out.println("删除用户...");
	        return 1;
	    }
	}

	//切面类
	public class MyAspect {
		//增强事务
	    public void before(){
	        System.out.println("开启事务");
	    }
	
	    public void after(){
	        System.out.println("提交事务");
	    }
	}

	//代理工厂类
	public class UserServiceFactory {
	    public static UserService createUserService(){
	
	        //1.创建目标对象target
	        UserService userService=new UserServiceImpl();
	
	        //2.声明切面类
	        MyAspect aspect=new MyAspect();
	
	        //3.切面类应用到目标类
	        //3.1 创建jdk代理
	        UserService serviceProxy=(UserService)Proxy.newProxyInstance(UserServiceFactory.class.getClassLoader(), userService.getClass().getInterfaces(), new InvocationHandler() {
	            @Override
	            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
	                //这个方法的返回值就是业务方法的返回值
	
	                //开启事务
	                aspect.before();
	                Object obj=method.invoke(userService,args);
	                //提交事务
	                aspect.after();
	                return obj;
	            }
	        });
	
	        return serviceProxy;
	    }
	}

	//测试类
	public class LessonTest {
	    @Test
	    public void test() throws Exception{
	        UserService userService= UserServiceFactory.createUserService();//得到一个代理对象(debug可以看到{$Proxy}的标记),代理对象中含有接口的实现类,故后面可以转成对应的接口。如果直接new的话得到的就是一个普通对象
	        userService.deleteUser(new User());
	        userService.addUser(new User());
	    }
	}

3.通过cglib实现
   2中的实现方法必须要有接口和对应的实现类,但是如果有的类并没有接口,就无法达到目的了。这个时候就要采用字节码增强框架cglib,在运行时创建目标类的子类,从而对目标类进行增强
   导入jar包:
	如果使用hibernate的话需要自己导包:
	核心:hibernate-distribution-3.6.10.Final\lib\bytecode\cglib\cglib-2.2.jar ,这个包需要下面的依赖
	依赖:struts-2.3.15.3\apps\struts2-blank\WEB-INF\lib\asm-3.3.jar
	spring-core..jar 已经整合以上两个内容

	■要被拦截的类
	public class StudentService {
	    public void add(){
	        System.out.println("添加学生...");
	    }
	
	    public void update(){
	        System.out.println("修改学生...");
	    }
	
	    public void delete(){
	        System.out.println("删除学生...");
	    }
	}

	■代理工厂
	public class StudentServiceLibFactory {
	
	    public static StudentService createStudentService(){
	        final StudentService studentService=new StudentService();
	        final MyAspect aspect=new MyAspect();
	
	        //创建增强类
	        Enhancer enhancer=new Enhancer();
	        //设置父类
	        enhancer.setSuperclass(studentService.getClass());
	        //设置回调(拦截)
	        enhancer.setCallback(new MethodInterceptor() {
	            @Override
	            public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
	                //proxy即为代理对象,是StudentService的子类
	                aspect.before();
	                Object retObj=method.invoke(studentService,args);
             	   //Object retObj=methodProxy.invokeSuper(proxy,args);//也可以这样写,解耦
	                aspect.after();
	                return retObj;
	            }
	        });
	
	        //创建代理对象
	        StudentService serviceProxy=(StudentService) enhancer.create();
	
	        return serviceProxy;
	
	    }
	}
	
	■测试类
	public class LessonTest {
	    @Test
	    public void test() throws Exception{
	        StudentService studentService= StudentServiceLibFactory.createStudentService();
	        studentService.add();
	    }
	}
	
4. Spring代理半自动
   4.1 AOP联盟通知类型
  	 AOP联盟为通知Advice定义了org.aopalliance.aop.Advice,Spring按照通知Advice在目标类方法的连接点位置,可以分为5类
		•前置通知 org.springframework.aop.MethodBeforeAdvice	在目标方法执行前实施增强
		•后置通知 org.springframework.aop.AfterReturningAdvice	在目标方法执行后实施增强
		•环绕通知 org.aopalliance.intercept.MethodInterceptor		在目标方法执行前后实施增强
		•异常抛出通知 org.springframework.aop.ThrowsAdvice		在方法抛出异常后实施增强
		•引介通知 org.springframework.aop.IntroductionInterceptor	在目标类中添加一些新的方法和属性
	导入jar:除了之前spring必须的5个包,还需要:
		com.springsource.org.aopalliance-1.0.0.jar	AOP联盟
		spring-aop-3.2.0.RELEASE.jar				AOP实现
	代码:
		■接口
		public interface UserService {
		    public void addUser(User user);
		    public void updateUser(User user);
		    public int deleteUser(User user);
		}
		■实现类
		public class UserServiceImpl implements UserService {
		    @Override
		    public void addUser(User user) {
		        System.out.println("添加用户...");
		    }
		
		    @Override
		    public void updateUser(User user) {
		        System.out.println("更新用户...");
		
		    }
		
		    @Override
		    public int deleteUser(User user) {
		        System.out.println("删除用户...");
		        return 1;
		    }
		}
		■切面类
		public class MySpringAspect implements MethodInterceptor {
		    @Override
		    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
		        //拦截方法
		        System.out.println("开启事务");	
		        //放行
		        Object retObj= methodInvocation.proceed();	
		        System.out.println("拦截...");
		        System.out.println("提交事务");	
		        return null;
		    }
		}
		■测试类
		public class LessonTest {	
		    @Test
		    public void test() throws Exception{
		        ApplicationContext context =new ClassPathXmlApplicationContext("beans1.xml");
		        UserService userService=(UserService) context.getBean("serviceProxy");
		        userService.updateUser(new User());
		    }
		}
		★配置文件
		<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans
		                           http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context   http://www.springframework.org/schema/context/spring-context.xsd">
		    <!--配置UserService-->
		    <bean id="userService" class="com.cn.service.UserServiceImpl"></bean>
		
		    <!--配置切面类对象-->
		    <bean id="myAspect2" class="com.cn.service.MySpringAspect"></bean>
		
		    <!--配置代理对象-->
		    <bean id="serviceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
		        <!--接口 一个接口就用value,多个接口用list。其实就是给ProxyFactoryBean的各个属性赋值-->
		        <property name="interfaces" value="com.cn.service.UserService"></property>
		        <!--目标对象  注意标签中的值是ref,下面的是value-->
		        <property name="target" ref="userService"></property>
		        <!--切面类-->
		        <property name="interceptorNames" value="myAspect2"></property>		
		    </bean>
		</beans>

5.Spring代理全自动
   5.1 导入jar:spring-framework-3.0.2.RELEASE-dependencies\org.aspectj\com.springsource.org.aspectj.weaver\1.6.8.RELEASE
   代码:
	★配置文件
	<?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.xsd
	                           http://www.springframework.org/schema/context
	                           http://www.springframework.org/schema/context/spring-context.xsd
	                           http://www.springframework.org/schema/aop							<!-- 添加 -->
	                           http://www.springframework.org/schema/aop/spring-aop.xsd">			<!-- 添加 -->
	
	    <!--配置UserService-->
	    <bean id="userService" class="com.cn.service.UserServiceImpl"></bean>
	
	    <!--配置切面类对象-->
	    <bean id="myAspect2" class="com.cn.service.MySpringAspect"></bean>
	
	    <!--全自动AOP配置
	        1.在bean中配置aop约束
	        2.配置aop:config内容。把切入点和通知结合
			proxy-target-class:true表示使用cglib实现代理
	    -->
	    <aop:config proxy-target-class="true">
	        <!--切入点
	            expression:表达式
	            下面的写法表示:最前面的*表示返回值,service下所有类(.*)的所有方法(.*),且不论什么参数((..))都会被织入代码
	        -->
	        <aop:pointcut id="myPointcut" expression="execution(* com.cn.service.*.*(..))"></aop:pointcut>
	        <!--通知 关联 切入点-->
	        <aop:advisor advice-ref="myAspect2" pointcut-ref="myPointcut"></aop:advisor>
	    </aop:config>
	</beans>
	■测试类(切面类,接口和实现类和上面一样)
	public class LessonTest {
	    @Test
	    public void test() throws Exception{
	        ApplicationContext context =new ClassPathXmlApplicationContext("beans1.xml");
	        UserService userService=(UserService) context.getBean("userService");			//直接使用该对象,拿到的即是一个代理对象了
	        userService.updateUser(new User());
	    }
	}
   5.2 继续添加jar包:spring-aspects-4.2.0	aspectj实现
   代码:
	■切面类(打印中的数字即是实际语句输出的数据)
	public class MyAspect2{
	    public void myBefore(){
	        System.out.println("1.前置通知...");
	    }
	
	    public void myAfterRunning(){
	        System.out.println("3.后置通知...");
	    }
	
	    //环绕事务需要放行,不然service中的事务无法执行。pjp 切入点,其实就是方法名
	    public Object myAround(ProceedingJoinPoint pjp) throws Throwable {
	        System.out.println("2.环绕通知...");
	        System.out.println("2.开启事务...");
	        //放行
	        Object retObj=pjp.proceed();			//该语句执行以后就会执行后置通知。所以4是出现在3后面的
	        System.out.println("4.提交事务...");
	        return retObj;
	    }
	
	     public void myAfterThrowing(JoinPoint jp,Throwable e){		//出现异常才会执行异常通知
		        System.out.println("异常通知:"+jp.getSignature().getName()+"==="+e.getMessage());
		}

		public void myAfter(JoinPoint jp){						//无论有没有异常最终都会走到最终通知
		        System.out.println("最终通知");
		}
	}
	■测试类(接口userservice和实现类不变)
	public class LessonTest {
	    @Test
	    public void test() throws Exception{
	        ApplicationContext context =new ClassPathXmlApplicationContext("beans1.xml");
	        UserService userService=(UserService) context.getBean("userService");
	        userService.updateUser(new User());
	    }
	}
	★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: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.xsd
	                           http://www.springframework.org/schema/context
	                           http://www.springframework.org/schema/context/spring-context.xsd
	                           http://www.springframework.org/schema/aop
	                           http://www.springframework.org/schema/aop/spring-aop.xsd">
	
	    <!--配置UserService-->
	    <bean id="userService" class="com.cn.service.UserServiceImpl"></bean>
	
	    <!--配置切面类对象-->
	    <bean id="myAspect2" class="com.cn.aspect.MyAspect2"></bean>
	
	    <!--配置aop-->
	    <aop:config>
	        <!--aop:指定切面-->
	        <aop:aspect ref="myAspect2">
	            <!--定一个切入点-->
	            <aop:pointcut id="myPointcut" expression="execution(* com.cn.service.UserServiceImpl.*(..))"></aop:pointcut>
	
	            <!--●配置前置通知和后置通知  pointcut-ref是对myPointcut的引用,事实上myPointcut可以不配置,直接将expression的值作为本属性的pointcut的值也可以,只不过之后的其他通知都要复用该切入点,故写出一个引用-->
	            <aop:before method="myBefore" pointcut-ref="myPointcut"></aop:before>
	            <aop:after-returning method="myAfterRunning" pointcut-ref="myPointcut"></aop:after-returning>
			<!--●配置环绕通知(环绕通知可以代替前置和后置通知,即前两者是单方面的通知,这个运行前后都会有)-->
	            <aop:around method="myAround" pointcut-ref="myPointcut"></aop:around>
			<!--●配置异常通知  e是切面类中myAfterThrowing抛出异常的参数名-->
          		<aop:after-throwing method="myAfterThrowing" pointcut-ref="myPointcut" throwing="e"></aop:after-throwing>
			<!--●配置最终通知-->
         	      <aop:after method="myAfter" pointcut-ref="myPointcut"></aop:after>
	        </aop:aspect>
	    </aop:config>
	</beans>
   5.3 其他知识点
	5.2中的前置和后置通知也可以拿到连接点的方法名,例:(后置通知类比前置通知)
		public void myBefore(JoinPoint jp){
		        System.out.println("前置通知..."+jp.getSignature().getName());
		    }
		public Object myAround(ProceedingJoinPoint pjp) throws Throwable {
		        System.out.println("焕然通知开启事务..."+pjp.getSignature().getName());
		        Object retObj=pjp.proceed();		//区别:ProceedingJoinPoint可放行,JoinPoint不可放行
		        return retObj;
		    }
	后置通知是可以拿到返回值的:
		切面类中的写法是:
		    public void myAfterRunning(JoinPoint jp,Object retValue){
		        System.out.println("后置通知...");
		        System.out.println("返回值:"+retValue);
		    }
		对应配置文件中需要添加配置returning,改为:
	            <aop:after-returning method="myAfterRunning" pointcut-ref="myPointcut" returning="retValue"></aop:after-returning>

    
6.AspectJ
   6.1 简介
	AspectJ是一个基于Java语言的AOP框架
	Spring2.0以后新增了对AspectJ切点表达式支持
	@AspectJ 是AspectJ1.5新增功能,通过JDK5注解技术,允许直接在Bean类中定义切面
	新版本Spring框架,建议使用AspectJ方式来开发AOP
	主要用途:自定义开发
   6.2 切入点表达式 	execution()  用于描述方法 【了解,参见5中的配置文件】
	语法:execution(修饰符  返回值  包.类.方法名(参数) throws异常)	【掌握】
		修饰符,一般省略
			public		公共方法
				*			任意
		返回值,不能省略
			void			返回没有值
			String		返回值字符串
			* 			任意
		包,[省略]
			com.gyf.crm			固定包
			com.gyf.crm.*.service	crm包下面子包任意 (例如:com.gyf.crm.staff.service)
			com.gyf.crm..			crm包下面的所有子包(含自己)
			com.gyf.crm.*.service..	crm包下面任意子包,固定目录service,service目录任意包
		类,[省略]
			UserServiceImpl			指定类
			*Impl					以Impl结尾
			User*					以User开头
			*						任意
		方法名,不能省略
			addUser					固定方法
			add*						以add开头
			*Do						以Do结尾
			*						任意
		(参数)
			()						无参
			(int)						一个整型
			(int ,int)					两个
			(..)						参数任意
		throws ,可省略,一般不写。
	其他【了解】
		匹配包或子包中的方法
		within(com.gyf.aop..*)
		匹配实现接口的代理对象中的方法
		this(com.gyf.aop.user.UserDAO)
		匹配实现接口的目标对象中的方法
		target(com.gyf.aop.user.UserDAO)
		匹配参数格式符合标准的方法
		args(int,int)
		对指定的bean所有的方法
		bean('userServiceId')

7.基于注解的AOP配置(注解配置的各个通知语句的输出顺序和xml配置有所不同,具体的参见视频上的研究:F:\BaiduNetdiskDownload\2019年4月黑马程序员教程\01-黑马IDEA版本Java基础+就业课程\4.框架\03.Spring\day04-20140427\03.XXX)
   ★配置文件:
	<?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.xsd
	                           http://www.springframework.org/schema/context
	                           http://www.springframework.org/schema/context/spring-context.xsd
	                           http://www.springframework.org/schema/aop
	                           http://www.springframework.org/schema/aop/spring-aop.xsd">
	    <!--●首先添加以下固定两句配置-->
	    <!--配置扫描注解的位置-->
	    <context:component-scan base-package="com.cn"/>
	    <!--配置aop注解生效-->
	    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
	
	    <!--●再添加aop配置-->
	    <!--aop配置-->
	    <aop:config>
	        <aop:aspect ref="myAspect"></aop:aspect>
	    </aop:config>
	</beans>
   ■切面类(注解配置)
	@Component		//◀
	@Aspect			//◀切面类加上这两个注解
	public class MyAspect2{
	    //▼声明一个公共的切入点
	    @Pointcut("execution(* com.cn.service.UserServiceImpl.*(..))")
	    public void myPointcut(){
	
	    }
	
	    @Before("execution(* com.cn.service.UserServiceImpl.*(..))")		//◀可以直接赋值切入点
	    public void myBefore(JoinPoint jp){
	        System.out.println("前置通知..."+jp.getSignature().getName());
	    }
	
	    @AfterReturning(pointcut="myPointcut()",returning = "retValue")	//◀也可以通过公共切入点赋值
	    public void myAfterRunning(JoinPoint jp,Object retValue){
	        System.out.println("后置通知...");
	        System.out.println("返回值:"+retValue);
	    }
	
	    @Around("myPointcut()")
	    public Object myAround(ProceedingJoinPoint pjp) throws Throwable {
	        System.out.println("环绕通知...");
	        System.out.println("开启事务..."+pjp.getSignature().getName());
	        //放行
	        Object retObj=pjp.proceed();
	        System.out.println("提交事务...");
	        return retObj;
	    }
	
	    @AfterThrowing(pointcut = "myPointcut()", throwing = "e")
	    public void myAfterThrowing(JoinPoint jp,Throwable e){
	        System.out.println("异常通知:"+jp.getSignature().getName()+"==="+e.getMessage());
	    }
	
	    @After("myPointcut()")
	    public void myAfter(JoinPoint jp){
	        System.out.println("最终通知");
	    }
	}
   ■测试类
	public class LessonTest {
	    @Test
	    public void test() throws Exception{
	        ApplicationContext context =new ClassPathXmlApplicationContext("beans1.xml");
	        UserService userService=(UserService) context.getBean("userService");
	        userService.updateUser(new User());
	    }
	}

8.注解总结
   @Aspect  声明切面,修饰切面类,从而获得 通知。
   通知:
	@Before 前置
	@AfterReturning 后置
	@Around 环绕
	@AfterThrowing 抛出异常
	@After 最终
   切入点
	@PointCut ,修饰方法 private void xxx(){}  之后通过“方法名”获得切入点引用



DOWN 返回