面向切面编程
CGLIB的动态代理
不需要接口,目标类有没有接口都可以
package com.kang;
import java.lang.reflect.Method;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
import com.kang.proxy.Target;
public class Main2 {
public static void main(String[] args) {
//cglib
//字节码增强器 在运行时,根据目标类的字节码文件生成
//代理
Enhancer enhancer = new Enhancer();
//指定目标类的字节码对象
//根据目标类派生出其代理类
enhancer.setSuperclass(Target.class);
//设置方法拦截器
enhancer.setCallback(new MethodInterceptor() {
/*
* 代理对象调用方法时,实际执行的就是Intercept方法
* 和JDK的invoke方法是一样的作用
* 前三个参数和返回值的意义也和JDK的一样
*
* 第四个参数:第二个参数的代理对象,用方法的代理对象
* 执行方法时,效率高一些。
*/
@Override
public Object intercept(Object proxy,
Method method,
Object[] args,
MethodProxy methodProxy) throws Throwable {
System.out.println("方法被拦截到了");
return null;
}
});
//创建代理对象
Object proxy = enhancer.create();
System.out.println(proxy.getClass());
}
}
//输出:class com.kang.proxy.Target$$EnhancerByCGLIB$$c528a51c
AOP
AOP概述:
面向切面的编程 Gof设计模式的延申
对OOP的一个补充,将工程中系统级别的功能统一实现、管理的一种思想
希望做到功能解耦合的目的
日志、事务等等
将这些系统级别的功能,封装到统一的地方,一般是类中,
将该类的对象在运行时织入到需要用到这些功能的地方
AOP名词解释
1.目标类(target class):具有核心功能的类,需要被增强功能的类
2.连接点:(join point):提前定义好的一些点,这些点可以被添加额外的功能
例如方法中、异常代码块等等
3.切入点(pointcut):根据一定的规则,筛选出来的,确定要添加额外功能的连接点
4.通知(advice):额外功能
1.前置通知(before):在核心方法执行之前执行
2.后置通知(after-return):在核心方法之后并且核心方法没有异常时执行,
3.后置异常通知(after-throwing):在核心方法发生异常之后执行
4.后置最终通知(after):在核心方法之后执行
5.环绕通知(around):最强大的 通知,通过环绕通知可以实现以上四种通知
5.切面(aspect):额外功能所在的类
6.代理类(proxy class):目标类所需要添加的额外功能,都是在代理类中进行添加
7.织入(weaver):在运行时,将额外功能添加到切入点的整个过程。
8.引入(introductions):在运行时,为现有的类添加额外的方法
配置文件
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd"
default-lazy-init="true">
<!-- 目标对象 -->
<bean id = "personDao" class = "com.kang.dao.impl.PersonDaoImpl"></bean>
<!-- 切面对象(通知对象) -->
<bean id = "myAspect" class = "com.kang.aspect.MyTransation"></bean>
<aop:config>
<!-- 切入点 -->
<aop:pointcut expression="" id=""/>
</aop:config>
</beans>
目标对象
package com.kang.dao;
public interface PersonDao {
public int add(String person);
public int remove(int id);
public int update(String name,int id);
public String search();
}
package com.kang.dao.impl;
import com.kang.dao.PersonDao;
public class PersonDaoImpl implements PersonDao {
@Override
public int add(String person) {
System.out.println("添加person"+person);
return 0;
}
@Override
public int remove(int id) {
System.out.println("删除person"+id);
return 0;
}
@Override
public int update(String name, int id) {
System.out.println("更新person"+id+":"+name);
return 0;
}
@Override
public String search() {
System.out.println("查询person");
return "查询结果";
}
}
切面对象(通知对象)
package com.kang.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
//切面 所有的通知都定义在切面中
public class MyTransation {
public void before() {
System.out.println("前置通知");
}
public void afterReturning() {
System.out.println("后置通知,无异常才执行");
}
public void afterThrowing() {
System.out.println("后置异常通知,有异常才执行");
}
public void after() {
System.out.println("后置最终通知,有没有异常都执行");
}
public void around(ProceedingJoinPoint proceed) {
//System.out.println("环绕通知");
try {
before();
//执行目标类中的对应的方法
proceed.proceed();
afterReturning();
} catch (Throwable e) {
afterThrowing();
e.printStackTrace();
} finally {
after();
}
}
}