(一)动态代理
动态代理是用来增强被代理类方法功能的一种方式,主要采用反射来实现。
package com.sgyj.java;
//实现动态代理
//动态代理是指 代理类是动态的,在使用代理类的时候,可以动态的决定使用哪个被代理类
//总结 其实逻辑就两步
// 第一个是获取代理类对象实例 Proxy.newProxyInstance(obj.getClass().getClassLoader(),obj.getClass().getInterfaces(),handler);
// 第二个就是代理类对象调用方法时 动态调用被代理的方法 public Object invoke(Object proxy, Method method, Object[] args)
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
interface Human{
public void believe();
public void eat(String food);
}
//被代理类
class superman implements Human{
@Override
public void believe() {
System.out.println("飞了");
}
@Override
public void eat(String food) {
System.out.println("我吃的是"+food);
}
}
//代理类
//1、如何根据加载到内存中的被代理类,动态的创建一个代理类及其对象
//2、当通过代理类对象调用方法时,如何动态的去调用被代理类中的同名方法
class proxyFactory{
public static Object getProxyInstance(Object obj){
MyInvocationHandler handler=new MyInvocationHandler();
handler.bind(obj);
//返回一个代理类对象 这个代理类对象代理的时obj类型的被代理类
return Proxy.newProxyInstance(obj.getClass().getClassLoader(),obj.getClass().getInterfaces(),handler);
}
}
class MyInvocationHandler implements InvocationHandler{
private Object obj;
public void bind(Object obj){
this.obj=obj;
}
//当我们通过代理类的对象,调用方法a时,就会自动的调用如下的方法:invoke()
//将被代理类要执行的方法a的功能就声明在invoke()中
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object returnvalue=method.invoke(obj,args);
return returnvalue;
}
}
public class ProxyTest {
public static void main(String[] args) {
//创建一个被代理类
superman s=new superman();
//创建一个代理类
Human p=(Human)proxyFactory.getProxyInstance(s);
p.believe();
p.eat("大米");
}
}
(二)AOP概念
(1)面向切面编程(方面),利用 AOP 可以对业务逻辑的各个部分进行隔离,从而使得
业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
(2)通俗描述:不通过修改源代码方式,在主干功能里面添加新功能
(三)原理与JDK动态代理实现
3.1原理
有两种动态代理
1、有接口的情况下,使用JDK动态代理:创建接口类的代理对象,增强类的方法
2、有继承的情况下,使用CGLIB动态代理:创建子类的代理对象,增强子类的方法
3.2JdK动态代理的实现
流程:
1、创建接口之后创建被代理类
2、创建代理类,代理类中做两件事,一件是获取被代理类对象,另一件是在myInvocation重写invoke方法来对被代理方法进行增强
关键词:proxy.newProxyInstance invoke
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
//代理类
public class UserProxy {
public static void main(String[] args) {
UserDaoImpl userDao=new UserDaoImpl();
Class [] interfaces={UserDao.class};
UserDao userDao1=(UserDao)Proxy.newProxyInstance(UserProxy.class.getClassLoader(),interfaces,new myInvocation(userDao));
double re=userDao1.add(2.1,51);
System.out.println(re);
userDao1.update();
}
}
class myInvocation implements InvocationHandler {
//创建的是谁的代理对象 就把谁传过来
private Object obj;
public myInvocation(Object obj){
this.obj=obj;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//实现增强代码
System.out.println(method.getName());
Object result = method.invoke(obj,args);
System.out.println("方法后执行"+result);
return result;
}
}
(四)AOP术语
- 连接点:哪些方法要增强,这些方法称为连接点
- 切入点:真正增强的方法称为切入点(更注重与方法名)
- 通知:实际增强的逻辑称为通知(更注重与具体的逻辑,比如权限逻辑)
- 切面:是一个过程,是一个具体实现。把通知应用到切入点。
(五)AOP的操作
Spring中AOP的实现是基于AspectJ实现AOP操作,AspectJ是独立于Spring框架的。
5.1使用注解方式实现
1、创建代理类和被代理类
2、在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.sgyj.aopanno">
</context:component-scan>
<!--开启Aspect生成代理-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
3、在代理类中,在作为通知方法上面添加通知类型注解,使用切入点表达式配置
@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("环绕之后.........");
} }
4、相同的切入点抽取
//相同切入点抽取
@Pointcut(value = "execution(* com.atguigu.spring5.aopanno.User.add(..))")
public void pointdemo() {
}
//前置通知
//@Before 注解表示作为前置通知
@Before(value = "pointdemo()")
public void before() {
System.out.println("before.........");
}
5、有多个增强类多同一个方法进行增强,设置增强类优先级
@Component
@Aspect
@Order(1)
public class PersonProxy
6、完全使用注解开发
@Configuration
@ComponentScan(basePackages = {"com.atguigu"})
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class ConfigAop {
}
5.2使用AspectJ配置文件
1、创建两个类,增强类和被增强类,创建方法
2、在 spring 配置文件中创建两个类对象
<bean id="book" class="com.atguigu.spring5.aopxml.Book"></bean> <bean id="bookProxy" class="com.atguigu.spring5.aopxml.BookProxy"></bean>
3、在 spring 配置文件中配置切入点
<!--配置 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:aspect>
</aop:config>