What is the AOP
AOP (Aspect Oriented Programming) means: section-oriented Programming, through pre-compilation and runtime dynamic proxy to achieve unified maintenance of program functions of a technology. AOP is a continuation of OOP, a hot topic in software development, and an important content in Spring framework. It is a derivative paradigm of functional programming. Using AOP, each part of the business logic can be isolated, thus reducing the degree of coupling between each part of the business logic, improving the reusability of the program, and improving the efficiency of development.
AOP’s role in Spring
Provide declarative transactions; Allows users to customize facets ==> similar to the proxy pattern in Java design pattern
Learn about some terms in Spring-AOP
Crosscutting concerns: Methods or functions that span multiple modules of an application. That is, the part that is irrelevant to our business logic, but that we need to focus on, is crosscutting concerns. Such as logging, security, caching, transactions and more….
ASPECT: A special object whose crosscutting concerns are modularized. That is, it is a class.
Advice: Work that must be done by the aspect. That is, it is a method in a class.
Target: indicates the notified object.
Proxy: Object created after notification is applied to the target object.
PointCut: The definition of the “place” where a tangent notification is executed.
Join Point: The execution point that matches the pointcut.
Use Spring to implement AOP
Three ways
- Through the Spring API interface
- This is done by custom classes
- This is done through annotations
Preparatory work: [Key points]
Importing AOP dependencies
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
Copy the code
Business interface
package service;
public interface Userervice {
public void add(a);
public void delete(a);
public void update(a);
public void select(a);
}
Copy the code
Business interface implementation class
package service;
public class UserserviceImpl implements Userervice{
public void add(a) {
System.out.println("Added a user.");
}
public void delete(a) {
System.out.println("Deleted a user");
}
public void update(a) {
System.out.println("Updated a user");
}
public void select(a) {
System.out.println("Query a user"); }}Copy the code
Through the Spring API interface
- Writing pre-log
package Log;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class BeforeLog implements MethodBeforeAdvice {
/* method: the method of the target object to be executed. Objects: the parameters of the called method
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println(o.getClass().getName() + "The" + method.getName() + "Carried out."); }}Copy the code
- Write post-log
package Log;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class AfterLog implements AfterReturningAdvice {
/ / the returnValue return values
//method Specifies the method to be called
//args is the argument to the object of the called method
//target Specifies the target object to be called
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("Executed."+target.getClass().getName()+"The"+method.getName()+"Method, return value is"+returnValue); }}Copy the code
- Write the applicationContext.xml core configuration file
[Note: Add AOP constraints]
<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 http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd ">
<! Registered bean -- -- -- >
<bean id="UserService" class="service.UserserviceImpl"/>
<bean id="beforeLog" class="Log.BeforeLog"/>
<bean id="afterLog" class="Log.AfterLog"/>< p style = "box-sizing: border-box! Important; word-wrap: break-word! Important;"<aop:config><-- configure pointcut ID: pointcut expression=" Execution (* service.userServiceImpl.*(..)) ": fixed expression specific usage to use when you can go to the Internet to check --><aop:pointcut id="point" expression="execution(* service.UserserviceImpl.*(..) )"/>
<aop:advisor advice-ref="beforeLog" pointcut-ref="point"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="point"/>
</aop:config>
</beans>
Copy the code
- The test class
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.Userervice;
public class DemoTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
Userervice userService = context.getBean("UserService", Userervice.class); userService.delete(); }}Copy the code
Implemented by custom classes
- Write custom classes
package diy;
public class diyPointCut{
public void before(a){
System.out.println("Method before execution");
}
public void after(a){
System.out.println("After method execution"); }}Copy the code
- Write the applicationContext.xml core configuration file
<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 http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd ">
<! Registered bean -- -- -- >
<bean id="UserService" class="service.UserserviceImpl"/>
<bean id="beforeLog" class="Log.BeforeLog"/>
<bean id="afterLog" class="Log.AfterLog"/>
<! Method 2: Customize pointcuts
<bean id="diypoint" class="diy.diyPointCut"/>
<aop:config>
<aop:aspect ref="diypoint">
<aop:pointcut id="diypointcut" expression="execution(* service.UserserviceImpl.*(..) )"/>
<aop:before method="before" pointcut-ref="diypointcut"/>
<aop:after method="after" pointcut-ref="diypointcut"/>
</aop:aspect>
</aop:config>
</beans>
Copy the code
3. Test classes [The test classes of the three ways are the same]
Through annotations
- Create the annotation implementation class
package annotation;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
@Aspect
public class AnnotationPoint {
@Before("execution(* service.UserserviceImpl.*(..) )"
public void before(a){
System.out.println("Method before execution");
}
@After("execution(* service.UserserviceImpl.*(..) )"
public void after(a){
System.out.println("After method execution");
}
@Around("execution(* service.UserserviceImpl.*(..) )"
public void around(ProceedingJoinPoint jp) throws Throwable {
System.out.println("Around the front");
Object proceed = jp.proceed();// Execute the target method proceed
System.out.println("Around the back"); }}Copy the code
- Write the applicationContext.xml core configuration file
<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 http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd ">
<! Registered bean -- -- -- >
<bean id="UserService" class="service.UserserviceImpl"/>
<bean id="beforeLog" class="Log.BeforeLog"/>
<bean id="afterLog" class="Log.AfterLog"/>
<! -- Way 3: Through annotations -->
<bean id="annotation" class="annotation.AnnotationPoint"/>
<aop:aspectj-autoproxy/>
</beans>
Copy the code
- The test class
- Through the AOP namespace
<aop:aspectj-autoproxy />
The declaration automatically creates proxies for beans in the Spring container that are configured with the @AspectJ aspect, woven into the aspect. , of course, the spring inside still adopt AnnotationAwareAspectJAutoProxyCreator automatically agent for the creation of the work, but the specific implementation details have been < aop: aspectj autoproxy / > is hidden- < AOP: Aspectj-autoproxy /> There is one
proxy-target-class
Property, the default isfalse, indicates usingJDK dynamic proxy injection enhancementsWhen the match<aop:aspectj-autoproxy poxy-target-class="true"/>
Is usedCGLib dynamic proxy technology injection enhancement.- But if the target class does not declare an interface, even if
proxy-target-class
Set to false, spring will also automatically use CGLib dynamic proxies.