Spring AOP concept
- (1) AOP (Aspect Oriented Programming) is
面向切面编程
.
It is a technology that realizes the unified maintenance of program functions through pre-compilation and runtime dynamic agents.
To put it simply, the function enhancement of the method is
essentially generated without changing the original code of the method.一个新的类,叫做代理类
- (2) AOP adopts a dynamic proxy method for program expansion. (
JDK动态代理
andCglib动态代理
two methods)
Spring dynamic proxy
- (1) JDK dynamic proxy
"methods of the
Proxy class" static methods of the Proxy class can create proxy objects
static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h)
" three parameters
Parameter 1: ClassLoader loader class loader, used to load the proxy object
Parameters 2: Class<?>[] interfaces target The bytecode object array of the class. Because the proxy is an interface, you need to know all the methods in the interface.
Parameter 3: InvocationHandler h execution handle, the core logic of proxy object processing is in this interface - (2) Case: CEOs eat,
CEOs,
secretary
TestJdkProxy
public class TestJdkProxy {
public static void main(String[] args) {
//Jdk代理
//ILaoZong
LaoZong laoZong = new LaoZong();
MiShu miShu = new MiShu();
//1 创建一个代理类,创建该类的对象
ClassLoader classLoader = LaoZong.class.getClassLoader();
Class[] interfaces= new Class[]{
ILaoZong.class};
//处理器
InvocationHandler handler = new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//调 laibeijiu()
miShu.laiBeiJiu();
//调 eat()
//laoZong.eat();
//method 被增加的方法
Object returnValue = method.invoke(laoZong,args);
//调 laiGenYan()
miShu.laiGenYan();
return returnValue;
}
};
ILaoZong iLaoZong = (ILaoZong) Proxy.newProxyInstance(classLoader,interfaces,handler);
iLaoZong.eat();
}
}
LaoZong
public class LaoZong implements ILaoZong{
public void eat(){
System.out.println("eat san xia guo");
System.out.println("eat wa wa cai");
}
}
ILaoZong
public interface ILaoZong {
void eat();
}
MiShu
public class MiShu {
public void laiBeiJiu(){
System.out.println("laiBeiJiu");
}
public void laiGenYan(){
System.out.println("laiGenYan");
}
}
Case: Log System
Add log to all methods of a class
Demo02
public class Demo02 {
public static void main(String[] args) {
Person p = new Person("jack","123456");
//生成代理类,创建该类对象
PersonDao2 personDao2 = new PersonDao2();
Logger logger = LoggerFactory.getLogger(PersonDao2.class);
ClassLoader classLoader=PersonDao2.class.getClassLoader();//与原来类一样
Class<?>[] interfaces=PersonDao2.class.getInterfaces();//与原来类一样
InvocationHandler handler = new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// 调用Dao类的update(person)
// 调用Logger类的debug(“update parameter return”)
//update delete add 的执行
//开始
long start = System.currentTimeMillis();
Object returnVal = method.invoke(personDao2,args);
long time = System.currentTimeMillis()-start;
logger.debug("方法名:"+method.getName()+" 参数"+ Arrays.toString(args)+" 返回值:"+returnVal+" 耗时"+time);
return returnVal;
}
};
IPersonDao2 personDao= (IPersonDao2) Proxy.newProxyInstance(classLoader,interfaces,handler);
// personDao.add(p);
personDao.update(p);
// personDao.delete(1);
}
}
PersonDao2
public class PersonDao2 implements IPersonDao2{
public void add(Person p){
System.out.println("执行 数据库的insert");
}
public void update(Person p){
System.out.println("执行 数据库的update");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void delete(int id){
System.out.println("执行 数据库的delete");
}
}
IPersonDao2
public interface IPersonDao2 {
void add(Person p);
void update(Person p);
void delete(int id);
}