Introduction to spring 3.2.2 AOP instances

Source: Internet
Author: User

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
Interface
Demointerface {

 

Public
Abstract
String getname ();

 

Public
Abstract void
Sava (string myname );

 

Public
Abstract void
Update (string myname, string hename );

 

}

 

Then write an instance for this interface

PackageCom. Fish. impl;

 

ImportCom. Fish. demointerface;

 

Public
Class
DemoImplementsDemointerface {

 

PublicString getname (){

Return
"Fish ";

}

 

Public
Void
Sava (string myname ){

System.Out. Println ("this is a save method ");

}

 

Public
Void
Update (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
Class
Myaop {

Public
Void
Mymthod1 (){

System.Out. Println ("pre-notification ");

}

Public
Void
Mymthod2 (){

System.Out. Println ("post notification ");

}

Public
Void
Mymthod3 (){

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
Class
Test {

Public
Static void
Main (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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.