1. What is AOP
In the software industry, AOP is the abbreviation of Aspect Oriented Programming, which means: aspect-oriented programming, a technology that realizes unified maintenance of program functions through pre-compilation and dynamic agents during runtime. AOP is a continuation of OOP , a hot spot in software development , an important content in the Spring framework, and a derivative paradigm of functional programming . AOP can be used to isolate various parts of business logic , thereby reducing the coupling degree between various parts of business logic, improving the reusability of programs , and improving the efficiency of development at the same time.
2. AOP related terms
Joinpoint (connection point):
The so-called connection points are those points that are intercepted. In spring, these points refer to methods, since spring only supports method type join points.
Pointcut (entry point):
The so-called entry point refers to the definition of which Joinpoints we want to intercept.
Advice (notification/enhancement):
The so-called notification means that the thing to do after intercepting the Joinpoint is the notification. Types of notifications: pre-notification, post-notification, exception notification, final notification, surround notification.
Introduction (introduction):
Introduction is a special notification. On the premise of not modifying the class code, Introduction can dynamically add some methods or Fields to the class during runtime.
Target (target object):
The proxy's target object.
Weaving (weaving):
Refers to the process of applying enhancements to a target object to create a new proxy object. Spring uses dynamic proxy weaving, while AspectJ uses compile-time weaving and class loading-time weaving.
Proxy:
After a class is enhanced by AOP weaving, a resulting proxy class is generated.
Aspect (section):
Is a combination of pointcut and notification (introduction).
3. Code example
1. First import the jar packages required by IOC and AOP
2. Create BookService interface
package com.yue.service;
public interface BooksService {
void findAll();
void add();
void delete();
void update();
}
3. Create BookServiceImpl to implement BookService inheritance. This class completes core function operations.
package com.yue.service.impl;
import com.yue.service.BooksService;
public class BookServiceImpl implements BooksService {
@Override
public void findAll() {
System.out.println("查询所有");
}
@Override
public void add() {
System.out.println("添加");
}
@Override
public void delete() {
System.out.println("删除");
}
@Override
public void update() {
System.out.println("修改");
}
}
4. Create a Logger class, which is used to enhance functions.
package com.yue.logger;
public class Logger {
public void preQuery(){
System.out.println("前置查询...preQuery");
}
public void postQuery(){
System.out.println("后置查询...postQuery");
}
public void exception(){
System.out.println("处理异常...exception");
}
public void treatment(){
System.out.println("最终处理...treatment");
}
}
5. Start AOP configuration in spring's 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">
<!--1.把所有类的对象交给IOC容器进行管理-->
<bean id="logger" class="com.yue.logger.Logger"/>
<bean id="bookService" class="com.yue.service.impl.BookServiceImpl"/>
<!--2.AOP的配置:让增强类 的 哪个方法 动态进行何种增强 核心类 的 哪个方法-->
<aop:config>
<aop:aspect id="log" ref="logger">
<aop:before method="preQuery" pointcut="execution(* *..BookServiceImpl.*(..))"/>
<aop:after-returning method="postQuery" pointcut="execution(* *..BookServiceImpl.*(..))"/>
<aop:after-throwing method="exception" pointcut="execution(* *..BookServiceImpl.*(..))"/>
<aop:after method="treatment" pointcut="execution(* *..BookServiceImpl.*(..))"/>
</aop:aspect>
</aop:config>
</beans>
6. Test whether the method of BookService is enhanced in the test class
package com.yue.servlet;
import com.yue.service.BooksService;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test01 {
@Test
public void test01(){
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
BooksService bookService = context.getBean(BooksService.class);
bookService.add();
System.out.println("---------------");
bookService.findAll();
System.out.println("---------------");
bookService.delete();
System.out.println("---------------");
bookService.update();
}
}
operation result: