AOP is short for Aspect Oriented Programming, meaning: Aspect-oriented programming (also called Aspect-Oriented ), the pre-compilation method and runtime dynamic proxy can be used to dynamically and uniformly add functions to the program without modifying the source code. AOP is actually a continuation of the gof design model. The design model tirelessly pursues decoupling between callers and callers. AOP can be said to be an implementation of this goal.
First, write an interface.
Because all AOP instance proxies are interfaces.
Demointerface. Java
PackageCom. fish;
Public
InterfaceDemointerface {
Public
AbstractString getname ();
Public
Abstract voidSava (string myname );
Public
Abstract voidUpdate (string myname, string hename );
}
Then write an instance for this interface
PackageCom. Fish. impl;
ImportCom. Fish. demointerface;
Public
ClassDemoImplementsDemointerface {
PublicString getname (){
Return
"Fish ";
}
Public
VoidSava (string myname ){
System.Out. Println ("this is a save method ");
}
Public
VoidUpdate (string myname, stringhename ){
System.Out. Println ("this is an update method ");
}
}
Then we write a common Java class as a proxy.
PackageCom. Fish. AOP;
Public
ClassMyaop {
Public
VoidMymthod1 (){
System.Out. Println ("pre-notification ");
}
Public
VoidMymthod2 (){
System.Out. Println ("post notification ");
}
Public
VoidMymthod3 (){
System.Out. Println ("final notification ");
}
}
Below we configure in XML
Spring. xml
<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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
Http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd">
<AOP: aspectj-autoproxy
//> // First enable proxy AOP
<Bean
Id ="Demo"Class ="Com. Fish. impl. Demo"> </Bean>
// Instantiate two classes
<Bean
Id ="Myaop"Class ="Com. Fish. AOP. myaop"> </Bean>
// Instantiate two classes
<AOP: config>
// Register AOP
<AOP: Aspect
Id ="Asp"
Ref ="Myaop">
// Write a plane. The ref is associated with the proxy class.
<AOP: pointcut
Expression ="Execution (* COM. Fish. impl. Demo. * (..)" // intercept the target object of the proxy. * Before com indicates that all COM is blocked. Class in the fish package. The second * indicates all the methods in the demo. () indicates the parameters of the methods.
Id ="Mycut"/>
<AOP: before
Method ="Mymthod1"
Pointcut-ref ="Mycut"/>
// Then, before indicates that the proxy function mymthod1 method is executed before the start point.
<AOP: aftermethod = "mymthod2" pointcut-ref = "mycut"/> // There is a forward slash, so it is behind the geological target object. . There are many other statuses.
</AOP: aspect>
</AOP: config>
</Beans>
Below I will write a test class
Test. Java
PackageCom. fish;
ImportOrg. springframework. Context. applicationcontext;
ImportOrg. springframework. Context. Support. classpathxmlapplicationcontext;
Public
ClassTest {
Public
Static voidMain (string [] ARGs ){
Applicationcontext context =NewClasspathxmlapplicationcontext ("Spring. xml ");
Demointerface demo = (demointerface) Context. getbean ("Demo"); // The proxy here is the interface, or an error of type conversion exception will be reported. It is not a demo but a demointerface interface.
Demo. Sava ("AA ");
}
}
So we can see that the result is:
Pre-notification
This is a method to save
Post notification
From this we can know the sequence of execution. AOP is actually a dynamic proxy. I will not talk about the proxy mode of the design mode in 23.