Spring day three: AOP related concepts [understanding]

1.1 AOP overview

1.1.1 What is AOP
AOP: The full name is (Aspect oriented Programing) that is, aspect-oriented programming.
Insert picture description here

  • In the software industry, AOP is
    the abbreviation of Aspect Oriented Programming, which means: aspect-oriented programming, a technology that achieves unified maintenance of program functions through pre-compilation and runtime dynamic agents. AOP is a continuation of OOP (Object Oriented Programming), a hot spot in software development, and an important content in the Spring framework, and a derivative paradigm of functional programming. The use of AOP can isolate the various parts of the business logic, so that the coupling between the various parts of the business logic is reduced, the reusability of the program is improved, and the development efficiency is improved.
    AOP adopts a horizontal extraction mechanism to replace the traditional vertical inheritance system. Classic applications of repetitive code: transaction management, performance monitoring, security checking, caching, logging, etc. Spring
    AOP is implemented in pure Java without the need for a special compilation process and class loader. Weaving enhanced code into target classes through proxy during runtime.
    AspectJ is an AOP framework based on the Java language. Starting with Spring 2.0, Spring
    AOP introduces support for Aspect. AspectJ extends the Java language and provides a specialized compiler. Provide horizontal code weaving at compile time

    Simply put, it is to extract the repetitive code of our program, when it needs to be executed, use the technology of dynamic proxy to enhance our existing methods without modifying the source code.

1.1.2 Principle of AOP

  • The bottom layer of aop will be implemented using a proxy mechanism.
  • Interface + implementation class: spring uses jdk's dynamic proxy proxy.
  • Implementation class: spring uses cglib bytecode enhancement.

1.1.3 Implementation of AOP

  • Using dynamic proxy technology, the selection framework of the proxy will decide which dynamic proxy technology to use according to whether the target class implements the interface.

1.1.4 AOP related terms

  • Joinpoint (connection point) The
    so-called connection point refers to the point that is intercepted. In spring, these points are designated as methods, because spring only supports method type connection points. The connection point has been enhanced. For example: addUser()

  • Pointcut) (pointcut) The
    so-called pointcut refers to our definition of intercepting those Joinpoints.

  • .target: The target class, the class that needs to be proxied. For example: UserService

  • .advice notification/enhancement, enhanced code. For example: after, before

  • Weaving: refers to the process of applying enhanced advice to the target object to create a new proxy object proxy.

  • .proxy proxy class

  1. Aspect: It is the combination of pointcut and advice.
    A line is a special aspect.
    An entry point and a notification form a special surface.
    Insert picture description here
    1.1.5 Specific application of AOP
  • 1.1 Target class
public interface UserService {
    
    
	
	public void addUser();
	public void updateUser();
	public void deleteUser();

}
  • 1.2 Noodles
public class MyAspect {
    
    
	
	public void before(){
    
    
		System.out.println("前面");
	}
	
	public void after(){
    
    
		System.out.println("后面");
	}

}
  • 1.3 Factory
public class MyBeanFactory {
    
    
	
	public static UserService createService(){
    
    
		//1 目标类
		final UserService userService = new UserServiceImpl();
		//2切面类
		final MyAspect myAspect = new MyAspect();
		/* 3 代理类:将目标类(切入点)和 切面类(通知) 结合 --> 切面
		 * 	Proxy.newProxyInstance
		 * 		参数1:loader ,类加载器,动态代理类 运行时创建,任何类都需要类加载器将其加载到内存。
		 * 			一般情况:当前类.class.getClassLoader();
		 * 					目标类实例.getClass().get...
		 * 		参数2:Class[] interfaces 代理类需要实现的所有接口
		 * 			方式1:目标类实例.getClass().getInterfaces()  ;注意:只能获得自己接口,不能获得父元素接口
		 * 			方式2:new Class[]{UserService.class}   
		 * 			例如:jdbc 驱动  --> DriverManager  获得接口 Connection
		 * 		参数3:InvocationHandler  处理类,接口,必须进行实现类,一般采用匿名内部
		 * 			提供 invoke 方法,代理类的每一个方法执行时,都将调用一次invoke
		 * 				参数31:Object proxy :代理对象
		 * 				参数32:Method method : 代理对象当前执行的方法的描述对象(反射)
		 * 					执行方法名:method.getName()
		 * 					执行方法:method.invoke(对象,实际参数)
		 * 				参数33:Object[] args :方法实际参数
		 * 
		 */
		UserService proxService = (UserService)Proxy.newProxyInstance(
								MyBeanFactory.class.getClassLoader(), 
								userService.getClass().getInterfaces(), 
								new InvocationHandler() {
    
    
									
									@Override
									public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    
    
										
										//前执行
										myAspect.before();
										
										//执行目标类的方法
										Object obj = method.invoke(userService, args);
										
										//后执行
										myAspect.after();
										
										return obj;
									}
								});
		
		return proxService;
	}

}
  • 1.4 test
@Test
	public void demo01(){
    
    
		UserService userService = MyBeanFactory.createService();
		userService.addUser();
		userService.updateUser();
		userService.deleteUser();
	}

Note: The material comes from the Internet, collect it and share it for free!

Guess you like

Origin blog.csdn.net/qq_43078445/article/details/104852261