XML-based AOP configuration-Go

Source: Internet
Author: User
Tags call back

Http://www.cnblogs.com/yangy608/archive/2010/11/14/1876839.html

AOP (aspect-oriented programming, aspect-oriented programming) can be said to complement and improve OOP (object-oriented programing, object-oriented programming). OOP introduces concepts such as encapsulation, inheritance, and polymorphism to create an object hierarchy that simulates a collection of public behavior. When we need to introduce public behavior to scattered objects, oop seems powerless. In other words, OOP allows you to define relationships from top to bottom, but it is not appropriate to define left-to-right relationships. such as logging capabilities. Log code is often spread horizontally across all object hierarchies, and has nothing to do with the core functionality of the objects it spreads to. This is true for other types of code, such as security, exception handling, and transparent persistence. This irrelevant code scattered around is called crosscutting (cross-cutting) code, and in OOP design, it leads to a lot of duplication of code, rather than the reuse of individual modules.

AOP, on the contrary, uses a technique called "crosscutting" that splits the encapsulated object interior and encapsulates public behavior that affects multiple classes into a reusable module, called "Aspect", or tangent. The so-called "cut-off", in short, is to encapsulate the logic or responsibility that is not related to the business, but for the common invocation of the business module, to reduce the repetitive code of the system, to reduce the coupling between modules, and to facilitate future operability and maintainability. AOP represents a horizontal relationship, if the "object" is a hollow cylinder, which encapsulates the properties and behavior of the object, then the method of plane-oriented programming is like a razor that cuts through the hollow cylinders to get inside the message. The cut-off aspect is the so-called "slice". Then it hands the cut-off slices with a clever capture of the heavens, leaving no traces.

Using "crosscutting" techniques, AOP divides software systems into two parts: core concerns and crosscutting concerns. The main process of business process is the core concern, and the part that has little relation is the crosscutting concern. One feature of crosscutting concerns is that they often occur in many of the core concerns and are essentially similar everywhere. such as permission authentication, logging, transaction processing. The role of AOP is to separate the various concerns in the system, separating the core concerns from the crosscutting concerns. As Avanade's senior program architect Adam Magee says, the core idea of AOP is "separating the business logic in the application from the generic services that support it." ”

The technology of AOP can be divided into two main categories: one is to use dynamic agent technology, to decorate the message by intercepting the message, to replace the execution of the original object behavior, and to use the static weaving method to introduce the specific syntax to create "slice", so that the compiler can weave the relevant "slices" during compilation. The code. However, the technical characteristics of AOP are the same, namely:

      1, join point: is an exact point of execution in the execution of a program, such as a method in a class. It is an abstract concept, and when implementing AOP, there is no need to define a join point.
      2, point Cut (pointcut): essentially a structure that captures the connection point. In AOP, you can define a point cut to capture calls to related methods.
      3, Advice (notification): Is the execution code for point cut, which is the specific logic for performing "facets".
      4, Aspect (tangent): Point cut and advice are combined as aspect, which resembles a class defined in OOP, but it represents more of a horizontal relationship between objects.
      5, introduce (introduced): introduces an additional method or property for an object, thus achieving the purpose of modifying the object structure. Some AOP tools refer to them as mixin.

      6, AOP Proxy: An object created by the AOP framework that can often be used as a substitute for a target object, while an AOP proxy provides more powerful functionality than the target object. The real situation is that when the application invokes the method of the AOP proxy, the AOP proxy will call back the method of the target object in its own method to complete the application invocation. A typical example of an AOP proxy is the transaction proxy bean in spring. Typically, the method of the target bean is not transactional, and the AOP proxy contains all the methods of the target bean, and these methods are strengthened into transactional methods. Simply put, the target object is the blueprint, the AOP agent is the target object's enhancement, on the target object's foundation, adds the property and the method, provides the more powerful function. The
target object contains a series of pointcuts. A pointcut can trigger a collection of processing connection points. Users can define pointcuts themselves, such as using regular expressions. The AOP agent wraps the target object and joins the processing at the pointcut. The processing that joins in the Pointcut makes the method function of the target object more powerful. Spring uses the JDK dynamic agent to implement the AOP proxy by default, primarily for the proxy interface. You can also use the Cglib proxy. Implement the proxy for the class, not the interface. If the business object does not implement an interface, the Cglib proxy is used by default. But interface-oriented programming is a good habit, try not to target specific classes of programming. Therefore, a business object should typically implement one or more interfaces.

7. Target object: An object that contains a connection point, also known as a proxy object.
8. Pre-notification (before advice): A notification performed before a connection point (Joinpoint), but this notification does not prevent execution before the connection point. The ApplicationContext uses <aop:before> elements to declare in <aop:aspect>.
9. After notification (after advice): Notifications executed when a connection point exits (whether it is a normal return or an abnormal exit). The ApplicationContext uses <aop:after> elements to declare in <aop:aspect>.
10, after return notification (after return advice): A connection point after the normal completion of the notification, does not include the case of throwing an exception. The ApplicationContext uses <after-returning> elements to declare in <aop:aspect>.
11. Surround Notification (Around advice): A notification that surrounds a connection point, similar to the Dofilter method of the filter in the servlet specification in the Web. You can customize the behavior before and after the call to the method, or you can choose not to execute. The ApplicationContext uses <aop:around> elements to declare in <aop:aspect>.

12. Notification after an exception is thrown (after throwing advice): Notification that is executed when the method throws an unexpected exit. The ApplicationContext uses <aop:after-throwing> elements to declare in <aop:aspect>.

SPRING2.0 currently only supports the use of method calls as Connection points (join point).

Spring defines pointcut syntax: excution (modifiers-pattern? Ret-type-pattern Declaring-type-pattern? Name-pattern (Param-pattern) Throws-pattern? )

In addition to Ret-type-pattern (that is, return type mode), Name-pattern (param-pattern) (name mode and parameter mode), other modes are optional. The return type mode determines that the return type of the method must match one connection point in turn (that is, a method). One of the most frequently used return type patterns is *, which represents the matching of any return type. If the return type is indicated, such as String, then only the connection point (method) that returns the string type is matched. The name pattern matches the method name. You can use the * wildcard character to represent all method names. In the parameter pattern, () means that a method that does not accept any arguments is matched, and (.. ) represents a method that matches any number of parameters. A pattern (*) represents a method that matches any type of parameter. Pattern (*,string) indicates a match: the first is an arbitrary parameter type, and the second must be a method of type String.

Modifiers-pattern: Operation Permissions for methods

Ret-type-pattern: Return value

Declaring-type-pattern: The package where the method resides

Name-pattern: Method Name

Parm-pattern: Name of parameter

Throws-pattern: Exception

Here is an example of defining a pointcut:

。 Execution of any public method:

Excution (Public * * (.. ))

。 Any method that starts with a set is executed:

Excution (* set* (.. ))

。 Execution of any method of the Accountservice interface:

Excution (* com.xyz.service.accountservice.* (.. ))

。 Define the execution of any method in the service package:

Excution (* com.xyz.service.*.* (.. ))

。 Define the execution of any method in a service package or a child package:

Excution (* com.xyz.service). *.*(。。 ))

-----------------------------------------------------Gorgeous divider Line----------------------------------------------

In the spring configuration file, all facets and notifications are configured in the <aop:config> tab, and a applicationcontext can contain multiple <aop:config> a <aop:config > can contain pointcut, advisor, aspect elements (note that this order must be).

1. Declare a slice

<aop:config>

<aop:aspect id= "Myaspect" ref= "Mybean" >

。。。。。

</aop:aspect>

</aop:config>

<bean id= "Mybean" class= "" >

。。。。。

</Bean>

Description: The slice is declared with <aop:aspect>, and the backing bean (the supporting bean) is referenced with ref.

2. Declare a point of entry

<aop:config>

<aop:pointcut id= "Mypointcut" expression= "excution (* com.service.*.* (..))" />

</aop:config>

3. Declare a Notice

SPRING2.0 supports the advisors concept through the <aop:advisors> element, and in most cases it will be used in conjunction with transaction advice in the following format:

<aop:config>

<aop:pointcut id= "MyService" expression= "excution (* com.xyz.service.*.* (..))" />

<aop:advisors pointcut-ref= "MyService" advice-ref= "Tx-advice"/>

</aop:config>

<txt:advice id= "Tx-advice" >

<tx:attributes>

<tx:method name= "inser*" propagation= "REQUIRED" rollback-for= "Exception"/>
<tx:method name= "updat*" propagation= "REQUIRED" rollback-for= "Exception"/>
<tx:method name= "delet*" propagation= "REQUIRED" rollback-for= "Exception"/>
<tx:method name= "process*" propagation= "REQUIRED" rollback-for= "Exception"/>
<tx:method name= "*" propagation= "SUPPORTS" read-only= "true"/>

</tx:attributes>

</txt:advice>

Description: Advisors to perform advice-ref referenced transactions when executing pointcut methods

XML-based AOP configuration-Go

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.

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.