First, what AOP is?
And OOP contrast, aspect-oriented, traditional OOP code logic is developed in a top-down process in a number of cross-cutting issues, president of Health, these cross-cutting issues and our main business logic will not be scattered in every code places, making it difficult to maintain, programming ideas AOP is the business logic and cross-cutting issues of separation, so as to achieve the purpose of decoupling, the development of high efficiency and reusability of code (the purpose is to reuse code, the code extracted public )
Two, AOP application scenarios
1, logging
2, the authority verification
3, the efficiency of inspection
4, transaction management
Three, springAop underlying technology
1, JDK dynamic proxies
2, CGLIB agent
Question: is woven into the compile-time or runtime were weaving?
----> run, generating byte code, and then loaded into the virtual machine, the JDK using the principle of reflection, ASM CGLIB-use principle.
Question: initialization period of time weaving weaving or get the object?
---- "initialization time, has been the target object agent, put into the spring container
Problem: spring AOP using dynamic proxy or jdk cglib default?
----- "It depends on the conditions, if the class that implements the interface is to use jdk. If you do not implement an interface, use cglib.
Fourth, the relationship between the spring AOP and AspectJ?
1, both of which are technical in order to achieve AOP this purpose, the emergence of, spring aop see the AspectJ programming style
--- "Here is a story that is, the original early spring aop used when programming style, people use it, very convenient, but people do not understand. Later, spring aop began a Aspectj access to programming style programming
Here's a little egg (if you know, please skip), set up to spend a spring, but do not use xml file, and the bean is injected into the container, and the container from coming out of the project.
First Step: Add spring-context-dependent
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.0.8.RELEASE</version> </dependency>
Second Step: Add three classes, a class test
@Configuration @ComponentScan("com.ving") public class AopConfig { } //////////////////////////////////////////////// @Component public class IndexDao { public void query(){ System.out.println("dao----query"); } } //////////////////////////////////////////////// public class Test { public static void main(String[] args) { AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(AopConfig.class); annotationConfigApplicationContext.start(); IndexDao bean = annotationConfigApplicationContext.getBean(IndexDao.class); bean.query(); } } //////////////////////////////////////////////// Print is: December 16, 2019 8:58:38 pm org.springframework.context.support.AbstractApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@6438a396: startup date [Mon Dec 16 20:58:38 CST 2019]; root of context hierarchy dao ---- query
Let's continue reading
Five, spring Aop applications
aspect section
Point cut (if you understand this concept cut point, in the possible application of a completely) represents a set of connection points (similar to a table)
Join point target object methods (each record)
weaving the proxy logic on the target object is added to the weaving process is called
advice notification type
The following is a section of code, the tangent point, connection point, notification of the four relations!
/ **
*
* cut
* Management must give spring
* /
@Component
the @Aspect
public class VingAspectJ {
/ **
* cut-off point
* Why cut point to be declared on a method? Purpose of the notes written on it
* pointcut It is set (that is, a collection of methods) connection points
* /
@Pointcut (. ". Execution (com.ving.dao * * * (..))")
public void PointCut () {
}
/ **
* notification - - "configure the tangent point
* /
@After (" com.ving.config.VingAspectJ.pointCut () ")
public void After () {
System.out.println (" After ");
}
@Before (" com.ving. config.VingAspectJ.pointCut () ")
public void before () {
System.out.println (" before ");
}
}