Spring AOP Finishing

Source: Internet
Author: User

Label:

Sample Show

AOP (Aspect oriented Programming) is a technology for Cutting-plane Programming. AOP is a useful complement to OOP based on the IOC Foundation.
AOP is widely recognized primarily because it splits the application system into 2 parts: the core business logic and the horizontal common logic, the so-called tangent plane. For example, all Medium-to-large applications involve persistent management, transaction management, rights management, log management, and debug Management. The use of AOP technology allows developers to focus only on the core business, while General logic uses AOP technology for horizontal cut-in, and the dedicated person to deal with these common logic, will make the task simple and clear, improve the efficiency of development and DEBUGGING.

Let's give an example (see the code on the right, for beginners only)

Sample Scenario

Scene: the singer sings the music, the audience wants to sit in the seat and closes the mobile phone, after the singing finishes, the audience satisfies on the applause, dissatisfied throws the unhappy Exception.
Analyzing this scene, a singer sings music is the core business logic, and before and after the Audience's movements are universal, belong to the horizontal general Logic.

Example schematic diagram

Implementation steps

1. First establish a singer interface performer.
2. To establish the Jazz singer class Dukeperformer to implement this interface, the class includes the Singer's personal Information.
3. Then define the audience class audience, including the Audience's behavior Notification.
4. Once completed, we can configure AOP facets in the configuration file Applicationcontext.xml. The key code is as Follows:

<aop:config><!--defining pointcuts--><aop:pointcut id= "sing" expression= "execution (* *.perform (..))" /><!--defining facets--><aop:aspect ref= "audience" ><!--pre-notification--><aop:before method= "takeseat" pointcut-ref= "sing"/><aop:before method= "turnoffphone" pointcut-ref= "SING"/><!--NOTIFY--><AOP after return: After-returning method= "applaud" pointcut-ref= "sing"/><!--thrown after notification--><aop:after-throwing method= "unHappy "pointcut-ref=" Sing "/></aop:aspect></aop:config>

5. Write the test class code, get the Bean object defined in spring, execute the perform performing method, and see how the background works.
Under normal circumstances, the console will print the following information:

The audience sat on the seat. Viewers turn off their phones. Duke began to sing Songs. The audience clapped, applause prolonged ...

Knowledge Preparation

What is aop?
AOP: (Aspect oriented Programming) programming for Facets. is a hot spot in software development at present, and also the spring Framework. AOP enables the isolation of parts of the business logic, which reduces the coupling between parts of the business logic, improves the reusability of the program, and improves the efficiency of development. The main functions are: logging, performance statistics, security control, transaction processing, exception handling and so On.

What is oop? What is the difference between aop?
AOP and oop are literally very similar, but they are two design ideas for different fields. OOP (object-oriented Programming) encapsulates the entity and its attributes and behaviors of the business process in an abstract way for clearer and more efficient logical unit PARTITIONING. AOP is the extraction of facets in the process of business processing, which is faced with a step or stage in the process of processing, in order to obtain the isolation effect of low coupling between parts of the logic process. These two kinds of design thought have the essential difference in the Goal.
In Layman's terms, oop is for the field of nouns, and AOP is for the verb domain.

How to understand aop?
For example, for example, if you want to check your pen for ink before each writing, it is common practice to call a method that determines whether there is ink in the action of Writing. One aspect of doing this is that the action of writing and whether there is ink are dependent, and if you have more than 10 different pens to write, every action in the pen should invoke the method of judging whether there is ink, and the other is: in terms of object-oriented programming, The method of writing and judging whether there is ink is the same level, if you let write this action to judge whether there is ink is not human, it is contrary to the idea of object-oriented program DESIGN.
If you use Spring's aop, it is to write the action as a facet, before each call different pen to write the method, call to determine whether there is ink method. It is defined by the <aop:config/> tag in the spring configuration File.

Resource Preparation

1:jar package: Spring Jar Package
2. Detailed study reference Spring reference manual Spring reference manual

Example

Implementations of AOP can use annotations and XML configuration files in two ways, using the XML configuration file Method. The sample process is shown in the "example show" on the left, below the source code:
First set up an interface performer class

1  package demoinfo.spring.aop; 2 3  public Interface Performer {4     // Performance Songs 5      public void perform (); 6 }
View Code

Then establish an interface implementation class Dukeperformer

1  packagedemoinfo.spring.aop;2 3  public classDukeperformerImplementsperformer{4     PrivateString name; 5      public voidsetName (String Name) {6          this. name=name; 7     }  8      publicString getName () {9         return  this. name; Ten     }   one      public voidperform () { aSystem.out.println ( this. name+ "started Singing Songs. ");  -     }   -}
View Code

audience audience Business class
1  packagedemoinfo.spring.aop;2 3  public classAudience {4      public voidtakeseat () {5System.out.println ("the audience sits on the Seat.") "); 6     }  7      public voidturnoffphone () {8System.out.println ("the Viewer shuts down the PHONE. "); 9     }  Ten      public voidapplaud () { oneSystem.out.println ("audience applause, Applause Prolonged ...");  a     }   -      public voidunhappy () { -System.out.println ("the audience is not Happy."));  the     }   -}
View Code

The configuration file Applicationcontext.xml code is as Follows:

1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Beansxmlns= "http://www.springframework.org/schema/beans"3 Xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"XMLNS:AOP= "http://www.springframework.org/schema/aop"4 xsi:schemalocation= "http://www.springframework.org/schema/beans5 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd6 HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP7 http://www.springframework.org/schema/aop/spring-aop-2.0.xsd ">8     <!--configuration of AOP learning -9     <BeanID= "dukeperformer"class= "demoinfo.spring.aop.DukePerformer">Ten         < propertyname= "name"value= "duke" /> one     </Bean> a     <BeanID= "audience"class= "demoinfo.spring.aop.Audience" /> -  -     <Aop:config> the         <!--Defining Pointcuts - -         <Aop:pointcutID= "sing"expression= "execution (* *.perform (..))"/> -         <!--Defining Facets - -         <Aop:aspectref= "audience"> +             <!--front-facing Notifications - -             <Aop:beforeMethod= "takeseat"Pointcut-ref= "sing" /> +             <Aop:beforeMethod= "turnoffphone"Pointcut-ref= "sing" /> a             <!--notify after return - at             <aop:after-returningMethod= "applaud"Pointcut-ref= "sing" /> -             <!--notification after throwing - -             <aop:after-throwingMethod= "unhappy"Pointcut-ref= "sing" /> -         </Aop:aspect> -     </Aop:config> -     <!--configuration of AOP learning - in </Beans>
View Code

The final Test class code is as Follows:

1  packagedemoinfo.spring.aop;2 3 Importorg.springframework.context.ApplicationContext; 4 Importorg.springframework.context.support.FileSystemXmlApplicationContext; 5 6  public classSpringdemo {7      public Static voidmain (string[] Args) {8         //get the bean instance (object) defined in spring9ApplicationContext ctx=NewFilesystemxmlapplicationcontext (Ten"classpath:demoinfo/spring/aop/applicationcontext.xml");  onePerformer Per= (performer) Ctx.getbean ("dukeperformer");  a Per.perform ();  -     }   -}
View Code

Spring AOP Finishing

Related Article

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.

Tags Index: