Spring(三)——AOP
概念
什么是AOP
(1)面向切面编程(方面),利用 AOP 可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
(2)通俗描述:不通过修改源代码方式,在主干功能里面添加新功能
AOP底层原理
- JDK动态代理(有接口情况):创建接口实现类代理对象,增强类的方法
- CGLIB动态代理(无接口情况):创建子类的代理对象,增强类的方法
JDK动态代理
1.使用 JDK 动态代理,使用 Proxy 类里面的方法创建代理对象
调用newProxyInstance 方法
- 第一参数,类加载器
- 第二参数,增强方法所在的类,这个类实现的接口,支持多个接口
- 第三参数,实现这个接口 InvocationHandler,创建代理对象,写增强的部分
2.编写JDK动态代理代码
(1)创建接口,定义方法- package com.atguigu.spring5;
- public interface UserDao {
- public int add(int a,int b);
- public String update(String id);
- }
复制代码 (2)创建接口实现类,实现方法- package com.atguigu.spring5;
- public class UserDaoImpl implements UserDao{
- @Override
- public int add(int a, int b) {
- return a+b;
- }
- @Override
- public String update(String id) {
- return id;
- }
- }
复制代码 (3)使用 Proxy 类创建接口代理对象- package com.atguigu.spring5;
- import java.lang.reflect.InvocationHandler;
- import java.lang.reflect.Method;
- import java.lang.reflect.Proxy;
- import java.util.Arrays;
- public class JDKProxy {
- public static void main(String[] args) {
- //创建接口实现类代理对象
- Class[] interfaces={UserDao.class};
- // 方法一:
- // Proxy.newProxyInstance(JDKProxy.class.getClassLoader(), interfaces, new InvocationHandler() {
- // @Override
- // public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
- //<?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.atguigu.spring5.aopanno"></context:component-scan>
-
- <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
- </beans>return null;
- // }
- // });
- UserDao userDao=new UserDaoImpl();
- UserDao dao=(UserDao)Proxy.newProxyInstance(JDKProxy.class.getClassLoader(), interfaces, new UserDaoProxy(userDao));
- int res=dao.add(1,2);
- System.out.println(res);
- }
- }
- //创建代理对象代码
- class UserDaoProxy implements InvocationHandler{
- //创建谁的代理对象就把谁传进来
- //有参构造传递
- private Object obj;
- public UserDaoProxy(Object obj){
- this.obj=obj;
- }
- //增强逻辑
- @Override
- public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
- //方法之前
- System.out.println("方法之前执行...."+method.getName()+" :传递的参 数..."+ Arrays.toString(args));
- //被增强的方法执行
- Object res = method.invoke(obj, args);
- //方法之后
- System.out.println("方法之后执行...."+obj);
- return res;
- }
- }
复制代码 AOP术语
1.连接点
类中哪些方法可以被增强,这些方法被称为连接点
2.切入点
实际被增强的方法叫切入点
3.通知(增强)
被增强的逻辑部分叫通知,下面是5种通知类型
- 前置通知
- 后置通知(返回通知)
- 环绕通知
- 异常通知
- 最终通知
4.切面
是一个动作,把通知应用到切入点的过程
AOP操作-准备工作
Spring框架一般基于AspectJ实现AOP操作
AspectJ 不是 Spring 组成部分,独立 AOP 框架,一般把 AspectJ 和 Spirng 框架一起使用,进行 AOP 操作
基于AspectJ实现AOP操作
(1)基于 xml 配置文件实现
(2)基于注解方式实现(使用)
引入AOP相关依赖
spring-aop-5.3.22.jar
com.springsource.net.sf.cglib-2.2.0.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.7.2.RELEASE.jar
切入点表达式
作用:知道对哪个类里面的哪个方法进行增强
语法结构: execution([权限修饰符] [返回类型] [类全路径] [方法名称] ([参数列表]) )
举例1:对 com.atguigu.dao.BookDao 类里面的 add 进行增强- //*表示任意修饰符public、private......(*后有空格)
- //..表示任意入参的方法
- execution(* com.atguigu.dao.BookDao.add(..))
复制代码 举例2:对 com.atguigu.dao.BookDao 类里面的所有的方法进行增强- execution(* com.atguigu.dao.BookDao.* (..))
复制代码 举例 3:对 com.atguigu.dao 包里面所有类,类里面所有方法进行增强- execution(* com.atguigu.dao.*.* (..))
复制代码 AspectJ注解操作
1.创建类,在类里定义方法
2.创建增强类,编写增强逻辑
在增强类里面,创建方法,让不同方法代表不同通知类型
3.进行通知配置
(1)在 spring 配置文件中引入名称空间,开启注解扫描
(2)使用注解创建 User 和 UserProxy 对象
(3)在增强类上面添加注解 @Aspect,生成代理对象
(4)在 spring 配置文件中开启生成代理对象
4.配置不同类型的通知
在增强类的里面,在作为通知方法上面添加通知类型注解,使用切入点表达式配置
User.java- package com.atguigu.spring5.aopanno;
- import org.aspectj.lang.annotation.Aspect;
- import org.springframework.stereotype.Component;
- @Component
- public class User {
- public void add(){
- System.out.println("User add..........");
- }
- }
复制代码 UserProxy.java- package com.atguigu.spring5.aopanno;
- import org.aspectj.lang.ProceedingJoinPoint;
- import org.aspectj.lang.annotation.*;
- import org.springframework.stereotype.Component;
- //增强类
- @Component
- @Aspect
- public class UserProxy {
- //前置通知
- //@Before 注解表示作为前置通知
- @Before(value = "execution(* com.atguigu.spring5.aopanno.User.add(..))")
- public void before() {
- System.out.println("before.........");
- }
- //后置通知(返回通知)
- @AfterReturning(value = "execution(* com.atguigu.spring5.aopanno.User.add(..))")
- public void afterReturning() {
- System.out.println("afterReturning.........");
- }
- //最终通知
- @After(value = "execution(* com.atguigu.spring5.aopanno.User.add(..))")
- public void after() {
- System.out.println("after.........");
- }
- //异常通知
- @AfterThrowing(value = "execution(* com.atguigu.spring5.aopanno.User.add(..))")
- public void afterThrowing() {
- System.out.println("afterThrowing.........");
- }
- //环绕通知
- @Around(value = "execution(* com.atguigu.spring5.aopanno.User.add(..))")
- public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
- System.out.println("环绕之前.........");
- //被增强的方法执行
- proceedingJoinPoint.proceed();
- System.out.println("环绕之后.........");
- }
- }
复制代码 TestAop.java- package com.atguigu.spring5.test;
- import com.atguigu.spring5.aopanno.User;
- import org.junit.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class TestAop {
- @Test
- public void testAopanno(){
- ApplicationContext context=new ClassPathXmlApplicationContext("bean1.xml");
- User user=context.getBean("user", User.class);
- user.add();
- }
- }
复制代码 bean1.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">
-
- <context:component-scan base-package="com.atguigu.spring5.aopanno"></context:component-scan>
-
- <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
- </beans>
复制代码 5.相同切入点抽取
多个方法execution表达式相同时使用
- //相同切入点抽取
- @Pointcut(value = "execution(* com.atguigu.spring5.aopanno.User.add(..))")
- public void pointdemo() {}
- @Before(value = "pointdemo()")
- public void before() {
- System.out.println("before.........");
- }
复制代码 6.有多个增强类对同一个方法进行增强,设置增强类优先级先后执行
在增强类上面添加注解 @Order(数字类型值),数字类型值越小优先级越高
7.完全注解开发
创建配置类- @Configuration
- @ComponentScan(basePackages = {"com.atguigu"})
- @EnableAspectJAutoProxy(proxyTargetClass = true)
- public class ConfigAop {}
复制代码 配置文件操作
1、创建两个类,增强类和被增强类,创建方法
2、在spring配置文件中创建两个类对象- [/code][b]3、在spring配置文件中配置切入点[/b]
- [code]<?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.atguigu.spring5.aopanno"></context:component-scan>
-
- <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
- </beans><?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.atguigu.spring5.aopanno"></context:component-scan>
-
- <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
- </beans>
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |