Spring (5) Brief Introduction to AOP and Spring AOP
I. Introduction to AOP
The full name of AOP is aspect-oriented programming, which is the core idea of aspect-oriented numbering,
AOP and OOP are both object-oriented programming languages that do not conflict with each other. They are two complementary design patterns.
The AOP technology makes up for the lack of object-oriented programming ideas. spring aop is a technology for Implementing aop, and srping aop is the core of a sub-framework or sub-function in the spring framework.
SPring containers do not depend on AOP.
This means that programmers can choose whether to use the aop technology by themselves. aop provides a powerful middleware solution, which makes spring ioc containers more comprehensive.
Ii. Terms 2.1
- Cross-cutting concern: service on the system layer is interspersed into the processing process of business logic.
- Aspect: place the application on demand and remove it from the application when not needed
- Advcie: the specific implementation of Aspect. advice includes the behavior of cross-cutting conerns or the services provided.
- Joinpoint: the time when Aspect joins the business process during application execution
- Pointcut: Specifies an aspect that is interspersed with applications during those joinpoints.
- Target: the object or Target object to which an advice is applied.
- Instruction: For compiled classes, add some methods dynamically during execution without modifying or adding any code.
- Weave: the process of being applied to objects.
2.2. Spring support for AOP
Written in pure Java language
You can use the configuration file to define pointcutes.
The jointpoints of attribute members is not supported. (The spring design thought that the jointpoints supporting attribute members will destroy the object encapsulation)
3. Create Advice3.1 and Before Advice by Spring
The method of the target object is called before execution.
Implement the MethodBeforeAdvice Interface
MethodBeforeAdvice inherits from BeforeAdvice, while BeforeAdvice inherits from the Advice interface. The before method is called and executed before the method specified by the target object is executed. Before returns void and does not return a value. After the before method is executed, the method of the target object is executed.
3.2. Instance
Package com. pb;/*** interface * @ author Administrator **/public interface IHello {public void sayHello (String str );}
Package com. pb;/*** interface implementation class * @ author Administrator **/public class Hello implements IHello {@ Override public void sayHello (String str) {System. out. println ("Hello" + str );}}
Proxy
Package com. pb; import java. lang. reflect. method; import org. springframework. aop. methodBeforeAdvice;/*** call @ author Administrator * before Method execution to implement the MethodBeforeAdvcie interface */public class implements MethodBeforeAdvice {@ Override public void before (Method arg0, object [] arg1, Object arg2) throws Throwable {System. out. println ("==== call before method execution ==== ");}}
ApplicationContext. 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: p = "http://www.springframework.org/schema/p" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> <! -- Create a target object instance --> <bean id = "hello" class = "com. pb. Hello"/> <! -- Set the Advice instance --> <bean id = "sayBeforeAdvice" class = "com. pb. SayHelloBeforeAdvice"/> <! -- Create a proxy object --> <bean id = "hellProxy" class = "org. springframework. aop. framework. ProxyFactoryBean"> <! -- Set proxy interface --> <property name = "proxyInterfaces"> <value> com. pb. IHello </value> </property> <! -- Set the target object instance --> <property name = "target"> <ref local = "hello"/> </property> <! -- Set the Advice instance --> <property name = "interceptorNames"> <list> <value> sayBeforeAdvice </value> </list> </property> </bean> </beans>
Test class
Package com. pb; import org. springframework. context. applicationContext; import org. springframework. context. support. classPathXmlApplicationContext;/*** Test class * @ author Administrator **/public class Test {public static void main (String [] args) {ApplicationContext context = new ClassPathXmlApplicationContext ("applicationContext. xml "); IHello iHello = (IHello) context. getBean ("hellProxy"); iHello. sayHello ("AOP ");}}
Result:
===== Call before method execution ===== Hello AOP
3.3. After Advice
Called after the method of the target object is executed.
Implement AfterReturningAdvice
Add
Package com. pb; import java. lang. reflect. method; import org. springframework. aop. optional;/** call * to implement the AfterRetruningAdvice interface */public class SayAfterAdvice implements when {@ Override public void afterReturning (Object arg0, Method arg1, Object [] arg2, object arg3) throws Throwable {System. out. println ("========= after the method is executed, call ======= ");}}
ApplicationContext. 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: p = "http://www.springframework.org/schema/p" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> <! -- Create a target object instance --> <bean id = "hello" class = "com. pb. Hello"/> <! -- Set beforAdvice instance --> <bean id = "sayBeforeAdvice" class = "com. pb. SayHelloBeforeAdvice"/> <! -- Set the AfterAdvice instance --> <bean id = "sayAfterAdvice" class = "com. pb. SayAfterAdvice"/> <! -- Create a proxy object --> <bean id = "hellProxy" class = "org. springframework. aop. framework. ProxyFactoryBean"> <! -- Set proxy interface --> <property name = "proxyInterfaces"> <value> com. pb. IHello </value> </property> <! -- Set the target object instance --> <property name = "target"> <ref local = "hello"/> </property> <! -- Set the Advice instance --> <property name = "interceptorNames"> <list> <value> sayBeforeAdvice </value> <value> sayAfterAdvice </value> </list> </property> </bean> </beans>
Test class unchanged. Result:
==== Call before the method is executed ===== Hello AOP ========== call after the method is executed ======
3.4. Around Advice
Execute corresponding operations between and after method execution
Interface MethodInterceptor
Package com. pb; import org. aopalliance. intercept. methodInterceptor; import org. aopalliance. intercept. methodInvocation; public class SayAroundAdvice implements MethodInterceptor {@ Override public Object invoke (MethodInvocation arg0) throws Throwable {System. out. println ("========= do something before method execution"); Object result = arg0.proceed (); System. out. println ("do something after method execution ========"); return result ;}}
Configuration File
<? 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: p = "http://www.springframework.org/schema/p" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> <! -- Create a target object instance --> <bean id = "hello" class = "com. pb. Hello"/> <! -- Set beforAdvice instance --> <bean id = "sayBeforeAdvice" class = "com. pb. SayHelloBeforeAdvice"/> <! -- Set the AfterAdvice instance --> <bean id = "sayAfterAdvice" class = "com. pb. SayAfterAdvice"/> <! -- Set the AroundAdvice instance --> <bean id = "sayRoundAdvice" class = "com. pb. SayAroundAdvice"/> <! -- Create a proxy object --> <bean id = "hellProxy" class = "org. springframework. aop. framework. ProxyFactoryBean"> <! -- Set proxy interface --> <property name = "proxyInterfaces"> <value> com. pb. IHello </value> </property> <! -- Set the target object instance --> <property name = "target"> <ref local = "hello"/> </property> <! -- Set the Advice instance --> <property name = "interceptorNames"> <list> <! -- <Value> sayBeforeAdvice </value> <value> sayAfterAdvice </value> --> <value> sayRoundAdvice </value> </list> </property> </bean> </beans>
3.5. THrowsAdvice
When an exception occurs, a service object is notified for processing.
Implement the ThrowsAdvice Interface
Package com. pb; import java. lang. reflect. method; import org. springframework. aop. throwsAdvice; public class SayThowsAdvice implements ThrowsAdvice {public void afterThrowing (Method method, Object [] objs, Object target, Throwable ta) {System. out. println ("exception occurred:" + ta + ":" + method );}}
4. XML Schema-based
Simplified code implementation
Easy Maintenance of corresponding programs
All elements are defined in <aop: config>.
5. Annotation-based
Annotate Java common classes with annotations