三、AOP
- 三、AOP
- AOP(==概念==)
- 1、什么是AOP
- AOP(==底层原理==)
- 1、AOP底层使用动态代理
- AOP(JDK动态代理)
- 1、使用JDK动态代理,使用Proxy里面的方法创建代理对象
- 2、编写 JDK动态代理代码
- (1)创建接口,定义方法
- 新建模块spring5_demo4/spring5/新建UserDao
- (2)创建接口实现类,实现方法
- spring5/新建UserDaoImpl
- (3)使用Proxy类创建接口代理
- spring5/新建JDKProxy.java
- AOP(==术语==)
- 1、连接点
- 2、切入点
- 3、通知(增强)
- 4、切面
- AOP操作(准备)
- 1、Spring框架一般基于AspectJ实现AOP操作
- 2、基于AspectJ实现AOP操作
- 3、在项目过程里面引入AOP相关依赖
- 4、切入点表达式
- AOP 操作(AspectJ==注解==)
- 1、创建类,在类里面定义方法
- spring5/新建aopanno包/新建User类
- 2、创建增强类(编写增强逻辑)
- aopanno包/新建UserProxy类
- 3、进行通知配置
- (1)在spring配置文件中,开启注解扫描
- src/新建bean.xml
- (2)使用注解创建User和UserProxy对象
- (3)在增强类上面添加注解@Aspect
- (4)在spring配置文件中开启生成代理对象
- bean1.xml
- 4、配置不同类型的通知
- UserProxy
- spring5/新建test/新建testAop
- UserProxy
- 执行
- 5、相同切入点抽取
- UserProxy
- 6、有多个增强类对同一方法进行增强,设置增强类优先级
- (1)在增强类上面添加注解@Order(数字类型值),数字类型值越小,优先级越高
- aopanno/新建UserProxy
- UserProxy
- 7、完全使用注解开发
- (1)创建配置类,不需要创建xml配置文件
- spring5/新建config包/新建ConfigAop
- AOP 操作(AspectJ配置文件)
- 1、创建两个类,增强类和被增强类,创建方法
- spring5/新建aopxml包/新建Book
- aopxml/新建BookProxy
- 2、在spring配置文件中创建两个类对象
- src/新建bean2.xml
- 3、在spring配置文件中配置切入点
- bean2.xml
- TestAop
说明
仅供学习交流使用,
三、AOP
AOP(概念)
1、什么是AOP
(1)面向切面编程(方面),利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
(2)通俗描述:不通过修改源码方式,在主干功能里面添加功能
(3)使用登录例子说明AOP
AOP(底层原理)
1、AOP底层使用动态代理
(1)有两种情况的动态代理
第一种 有接口情况,使用JDK动态代理
- 创建接口实现类代理对象,增强类的方法
第二种 没有接口情况,使用CGLIB动态代理
- 创建子类的代理对象,增强类的方法
AOP(JDK动态代理)
1、使用JDK动态代理,使用Proxy里面的方法创建代理对象
java.lang.reflect
Class Proxy
java.lang.Object
java.lang.reflect.Proxy
(1) 调用 newProxyInstance方法
static Object newProxyInstance(ClassLoader loader,类<?>[] interfaces,InvocationHandler h)
返回给定接口的代理类的实例,该接口将方法调用分派给指定的调用处理程序。
方法里面有三个参数:
第一参数,类加载器
第二参数,增强方法所在的类,这个类实现的接口,支持多个接口
第三参数,实现这个接口InvocationHandler ,创建代理对象,写增强的方法
2、编写 JDK动态代理代码
(1)创建接口,定义方法
新建模块spring5_demo4/spring5/新建UserDao
package com.atguigu.spring5;
public interface UserDao {
public int add(int a,int b);
public String update(String id);
}
(2)创建接口实现类,实现方法
spring5/新建UserDaoImpl
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类创建接口代理
spring5/新建JDKProxy.java
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 {
// return null;
// }
// });
UserDaoImpl userDao =new UserDaoImpl();
UserDao dao = (UserDao) Proxy.newProxyInstance(JDKProxy.class.getClassLoader(), interfaces, new UserDaoProxy(userDao));
int result = dao.add(1, 2);
System.out.println("result:"+result);
}
}
//创建代理对象代码
class UserDaoProxy implements InvocationHandler{
//1 把创建的是谁的代理对象,就把谁传递过来
//有参数构造传递
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、通知(增强)
(1)实际增强的逻辑部分称为通知(增强)
(2)通过有多种类型
- 前置通知
- 后置通知
- 环绕通知
- 异常通知
- 最终通知 finally
4、切面
是动作
(1)把通知应用到切入点过程
AOP操作(准备)
1、Spring框架一般基于AspectJ实现AOP操作
(1)什么是AspectJ
- AspectJ不是Spring组成部分,独立AOP创建,一般把AspectJ和Spring框架一起使用,进行AOP操作
2、基于AspectJ实现AOP操作
(1)基于xml配置文件
(2)基于注解方式(使用)
3、在项目过程里面引入AOP相关依赖
spring-aspects-5.3.10.jar
com.springsource.net.sf.cglib-2.2.0
com.springsource.org.aopalliance-1.0.0
com.springsource.org.aspectj.weaver-1.6.0
4、切入点表达式
(1)切入点表达式作用:知道对哪个类里面的哪个方法进行增强
(2)语法结构:
execution([权限修饰符] [返回类型] [类全路径] [方法名称] ([参数列表]))
举例1:对com.atguigu.dao.BookDao类里面的add进行增强
execution(* com.atguigu.dao.BookDao.add(…))
举例2:对com.atguigu.dao.BookDao类里面的所有的方法进行增强
execution(* com.atguigu.dao.BookDao.*(…))
举例3:对com.atguigu.dao包里面所有类里面的所有的方法进行增强
execution(* com.atguigu.dao.*.*(…))
AOP 操作(AspectJ注解)
1、创建类,在类里面定义方法
spring5/新建aopanno包/新建User类
package com.atguigu.spring5.aopanno;
//被增强类
public class User {
public void add(){
System.out.println("add........");
}
}
2、创建增强类(编写增强逻辑)
(1)在增强类里面创建方法,让不同方法代表不同通知类型
aopanno包/新建UserProxy类
package com.atguigu.spring5.aopanno;
//增强类
public class UserProxy {
//前置通知
public void before(){
System.out.println("before........");
}
}
3、进行通知配置
(1)在spring配置文件中,开启注解扫描
src/新建bean.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>
</beans>
(2)使用注解创建User和UserProxy对象
(3)在增强类上面添加注解@Aspect
(4)在spring配置文件中开启生成代理对象
bean1.xml
<!-- 开启Aspect生成代理对象 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
4、配置不同类型的通知
(1)在增强类的里面在作为通知方法上面添加通知类型注解,使用切入点表达式配置
UserProxy
package com.atguigu.spring5.aopanno;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
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........");
}
}
spring5/新建test/新建testAop
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();
}
}
UserProxy
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........");
}
//最终通知
@After(value = "execution(* com.atguigu.spring5.aopanno.User.add(..))")
public void after(){
System.out.println("after........");
}
//后置通知(返回通知)
@AfterReturning(value = "execution(* com.atguigu.spring5.aopanno.User.add(..))")
public void afterReturning(){
System.out.println("afterReturning........");
}
//异常通知
@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("环绕之后........");
}
}
执行
环绕之前........
before........
add........
afterReturning........
after........
环绕之后........
5、相同切入点抽取
UserProxy
//相同切入点抽取
@Pointcut(value = "execution(* com.atguigu.spring5.aopanno.User.add(..))")
public void pointdoem(){
}
//前置通知
// @Before 注解表示作为前置通知
@Before(value = "pointdoem()")
public void before(){
System.out.println("before........");
}
6、有多个增强类对同一方法进行增强,设置增强类优先级
(1)在增强类上面添加注解@Order(数字类型值),数字类型值越小,优先级越高
aopanno/新建UserProxy
package com.atguigu.spring5.aopanno;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Component
@Aspect
@Order(1)
public class PersonProxy {
//前置通知
@Before(value = "execution(* com.atguigu.spring5.aopanno.User.add(..))")
public void afterReturning(){
System.out.println("Person Before........");
}
}
UserProxy
7、完全使用注解开发
(1)创建配置类,不需要创建xml配置文件
spring5/新建config包/新建ConfigAop
package com.atguigu.spring5.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@ComponentScan(basePackages = {"com.atguigu"})
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class ConfigAop {
}
AOP 操作(AspectJ配置文件)
1、创建两个类,增强类和被增强类,创建方法
spring5/新建aopxml包/新建Book
package com.atguigu.spring5.aopxml;
public class Book {
public void buy(){
System.out.println("buy.........");
}
}
aopxml/新建BookProxy
package com.atguigu.spring5.aopxml;
public class BookProxy {
public void before(){
System.out.println("before.........");
}
}
2、在spring配置文件中创建两个类对象
src/新建bean2.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">
<!-- 创建对象 -->
<bean id="book" class="com.atguigu.spring5.aopxml.Book"></bean>
<bean id="bookProxy" class="com.atguigu.spring5.aopxml.BookProxy"></bean>
</beans>
3、在spring配置文件中配置切入点
bean2.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">
<!-- 创建对象 -->
<bean id="book" class="com.atguigu.spring5.aopxml.Book"></bean>
<bean id="bookProxy" class="com.atguigu.spring5.aopxml.BookProxy"></bean>
<!--【配置aop增强-->
<aop:config>
<!--切入点-->
<aop:pointcut id="p" expression="execution(* com.atguigu.spring5.aopxml.Book.buy(..))"/>
<!--配置切面-->
<aop:aspect ref="bookProxy">
<!-- 增强作用在具体的方法上 -->
<aop:before method="before" pointcut-ref="p"></aop:before>
</aop:aspect>
</aop:config>
</beans>
TestAop
@Test
public void testAopXml(){
ApplicationContext context=new ClassPathXmlApplicationContext("bean2.xml");
Book book = context.getBean("book", Book.class);
book.buy();
}