目录
AOP
(1)面向切面编程(方面),利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高开发效率。
AOP方式:不通过修改原代码方式,在主干功能里面添加新功能。
AOP(底层原理)
1、aop底层使用动态代理。
(1)有接口情况,使用JDK动态代理。
*创建UserDao接口的实现类代理对象,增强类的方法
(2)没有接口情况,使用CGLIB动态代理
*创建子类的代理对象,增强类的方法
AOP(JDK动态代理)
1、使用JDK动态代理,使用Proxy类里面的方法创建代理对象。
(1)调用newProxyInstance方法
方法三个参数:
*类加载器;
*增强方法所在的类,这个类实现的接口,支持多个接口;
*实现这个接口InvocationHandler,创建代理对象,写增强方法。
2、编写动态代理代码
(1)创建接口,定义方法
public interface UserDao {
public int add(int a,int b);
public void update(String id);
}
(2)创建接口实现类,并实现方法
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 spring5_2;
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();
//第二种创建代理对象的代码,第三个参数用类实现接口
Class[] interfaces = {UserDao.class};
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 {
//第一步,把创建的是谁的代理对象,就把谁传递过来、
//有参构造传递
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)通知有多种类型
- 前置通知
- 后置通知
- 环绕通知
- 异常通知
- 最终通知
4、切面
是动作,把通知应用到切入点的过程。
AOP操作
1、Spring框架一般基于AspectJ实现AOP操作
AspectJ:不属于Spring的独立AOP框架,把AspectJ和Spring一起使用来进行AOP操作。
2、基于AspectJ实现AOP操作:基于XML配置文件;基于注解。
3、引入AOP依赖jar包
4、切入点表达式
(1)切入点表达式作用,知道对哪个类的哪个方法进行增强,
(2)语法结构
execution([权限修饰符][返回类型][类全路径][方法名称][(参数列表]))
例子1:对spring5_2.dao.BookDao类里面的add方法进行增强
execution(*spring5_2.dao.BookDao.add(..) )
例子2:对spring5_2.dao.BookDao类里面的所有方法进行增强
execution(*spring5_2.dao.BookDao.*(..) )
例子3:对spring5_2.dao包中所有类的所有方法进行增强
execution(*spring5_2.dao.*.*(..) )
AOP操作(第一种,AspectJ注解)
1、创建类,在类中定义方法
2、创建被增强类,编写增强逻辑
(1)在增强类中创建不同方法,代表不同通知类型
//增强的类
public class UserProxy {
//前置通知
public void before() {
System.out.println("before....");
}
}
3、进行通知的配置
(1)在Spring配置文件中开启注解扫描(完全注解/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="spring5_2.aopanno"></context:component-scan>
</beans>
(2)使用注解创建User和UserProxy对象
(3)在增强类上面增加注解@Aspect
(4)在Spring配置文件中开启生成代理对象
<!--开启Aspect生成代理对象-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
4、配置不同类型的通知
(1)在增强类中,作为通知方法上面添加通知类型注解,使用切入点表达式配置
//增强的类
@Component
@Aspect //生成代理对象
public class UserProxy {
//@Before 前置通知
@Before(value = "execution(* spring5_2.aopanno.User.add(..))")
public void before() {
System.out.println("before....");
}
//@After 最终通知 (有异常也会通知)
@After(value = "execution(* spring5_2.aopanno.User.add(..))")
public void after() {
System.out.println("after...表示after示在被增强方法之后执行");
}
//@AfterReturning 后置通知(返回通知)
@AfterReturning(value = "execution(* spring5_2.aopanno.User.add(..))")
public void afterReturning() {
System.out.println("afterReturning...表示返回值之后执行");
}
//异常通知。被增强方法有异常时才会执行它,比如原方法中有除以0的错误
@AfterThrowing(value = "execution(* spring5_2.aopanno.User.add(..))")
public void AfterThrowing() {
System.out.println("AfterThrowing...");
}
//Around 环绕通知
@Around(value = "execution(* spring5_2.aopanno.User.add(..))")
public void Around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("before Arounding...");
//此处填写被增强的方法
proceedingJoinPoint.proceed();
System.out.println("after Arounding");
}
}
5、相同的切入点抽取(有多个execution一模一样时,用注解@Pointcut抽取)
//相同切入点,抽取
@Pointcut(value = "execution(* spring5_2.aopanno.User.add(..))")
public void pointDemo(){
}
//@Before 前置通知
@Before(value = "pointDemo()")
public void before() {
System.out.println("before....");
}
6、有多个增强类对同一个方法增强时,可以设置增强类的优先级(@Order(数字))
//增强的类
@Component
@Aspect
@Order(1)
public class UserProxy {
//@Before 前置通知
@Before(value = "execution(* spring5_2.aopanno.User.add(..))")
public void before() {
System.out.println("before....");
}
//增强的类
@Component
@Aspect
@Order(1)
public class UserProxy1 {
//@Before 前置通知
@Before(value = "execution(* spring5_2.aopanno.User.add(..))")
public void before() {
System.out.println("before....");
}
...
//增强的类
@Component
@Aspect
@Order(2)
public class UserProxy2 {
//@Before 前置通知
@Before(value = "execution(* spring5_2.aopanno.User.add(..))")
public void before() {
System.out.println("before....");
}
...
7、创建配置类就不需要创建xml(scan)配置了。
@Configurable
@ComponentScan(basePackages = {"config"})
@EnableAspectJAutoProxy(proxyTargetClass =true)
AOP操作(第二种,AspectJ配置文件)
1、创建两个类,增强类和被增强类,创建方法
2、在Spring配置文件中创建两个类对象
<bean id="book" class="aopxml.Book"></bean>
<bean id="bookProxy" class="aopxml.BookProxy"></bean>
3、在spring配置文件中配置切入点
<!--配置AOP增强-->
<aop:config>
<!--切入点-->
<aop:pointcut id="p" expression="execution(* aopxml.Book.buy(..))"/>
<!--配置切面-->
<aop:aspect ref="bookProxy">
<!--增强作用在的具体方法上-->
<aop:before method="before" pointcut-ref="p"></aop:before>
</aop:aspect>
</aop:config>
<!--配置AOP增强-->
<aop:config>
<!--切入点-->
<aop:pointcut id="p" expression="execution(* aopxml.Book.buy(..))"/>
<!--配置切面-->
<aop:aspect ref="bookProxy">
<!--增强作用在的具体方法上-->
<aop:before method="before" pointcut-ref="p"></aop:before>
</aop:aspect>
</aop:config>