XML-based AOP Configuration

Source: Internet
Author: User

AOP (Aspect-Oriented Programming) can be said to be a supplement and perfection of OOP (object-oriented programing, object-oriented programming. Oop introduces concepts such as encapsulation, inheritance, and Polymorphism to establish an object hierarchy to simulate a set of public behaviors. When we need to introduce public behavior to scattered objects, oop seems powerless. That is to say, oop allows you to define the relationship from top to bottom, but it is not suitable for defining the relationship from left to right. For example, log function. Log Code is often horizontally distributed across all object layers, but it has nothing to do with the core functions of the objects it spreads. This is also true for other types of code, such as security, exception handling, and transparent persistence. This unrelated code distributed everywhere is called cross-cutting code. in OOP design, it leads to a large number of code duplication, which is not conducive to the reuse of each module.

The AOP technology is the opposite. It uses a technology called "cross-cutting" to segment the encapsulated object, encapsulate the public behaviors that affect multiple classes into a reusable module and name it "aspect", that is, the aspect. The so-called "aspect" is simply to encapsulate the logic or responsibilities that are irrelevant to the business but are called by the business module to reduce repeated system code, reduce the coupling between modules and facilitate future operability and maintainability. AOP represents a horizontal relationship. If "object" is a hollow cylinder that encapsulates the attributes and behaviors of objects, then the method for Aspect-Oriented Programming, it is like a sharp blade that splits these hollow cylinders to obtain internal messages. The cut section is also called the cut section. Then it restored these cut sections with the skill of winning the power of heaven without leaving any trace.

Using the "cross-cutting" technology, AOP divides the software system into two parts: the core focus and the cross-cutting focus. The main process of business processing is the core focus, and the part that has little to do with it is the cross-cutting focus. One feature of cross-cutting concerns is that they often occur in multiple places of core concerns, and are similar everywhere. For example, permission authentication, logs, and transaction processing. The function of AOP is to separate the various concerns in the system from the core concerns and the cross-cutting concerns. As Adam Magee, avanade's senior Solution Architect, said, the core idea of AOP is to "separate the business logic in applications from the general services it supports ."

The technology for Implementing AOP is mainly divided into two categories: one is to use dynamic proxy technology, and use the method of intercepting messages to describe the message to replace the execution of the original object behavior; the second is to use static weaving to introduce specific syntax to create "cut-plane", so that the compiler can weave the "cut-plane" code during compilation. However, the same way is achieved, and the technical features for Implementing AOP are the same:

1. Join point: a precise execution point in program execution, such as a method in the class. It is an abstract concept. When Implementing AOP, you do not need to define a join point.
2. point cut: essentially a structure that captures the connection points. In AOP, you can define a point cut to capture calls to related methods.
3. Advice (notification): indicates the Execution Code of the point cut and the specific logic for executing the "aspect.
4. aspect: the combination of point cut and advice is aspect. It is similar to a class defined in OOP, but it represents more of a horizontal relationship between objects.
5. Introduce (Introduction): introduces additional methods or attributes to an object to modify the object structure. Some AOP tools call it Mixin.

6. AOP Proxy: the object created by the AOP framework. This object can be used as a substitute for the target object. The AOP proxy provides more powerful functions than the target object. The actual situation is that when the application calls the method of the AOP proxy, the AOP proxy calls back the method of the target object in its own method to complete the application call. A typical example of AOP proxy is the transaction proxy bean in spring. Generally, the method of the target bean is not transactional, And the AOP proxy contains all the methods of the target bean, and these methods have been enhanced into transactional methods. Simply put, the target object is the blueprint, and the AOP proxy is the enhancement of the target object. Based on the target object, attributes and methods are added to provide more powerful functions.
The target object contains a series of entry points. The starting point can trigger the processing of the collection of connection points. You can define the entry point by yourself, for example, using a regular expression. The AOP proxy wraps the target object and adds processing to the entry point. The processing added to the entry point makes the method of the target object more functional. By default, spring uses JDK dynamic proxy to implement AOP proxy, which is mainly used for proxy interfaces. You can also use the cglib proxy. The proxy of the implementation class, not the interface. If the business object does not implement interfaces, cglib proxy is used by default. But interface-oriented programming is a good habit. do not apply to specific classes as much as possible. Therefore, a business object must implement one or more interfaces.

7. Target object: an object that contains a connection point, also known as a proxy object.
8. Before advice: A notification executed before a connection point. However, this notification cannot be executed before a connection point. In applicationcontext, use the <AOP: Before> element in <AOP: aspect> for declaration.
9. After notification (after advice): The Notification executed when a connection point exits (whether it is a normal return or an abnormal exit ). In applicationcontext, use the <AOP: After> element in <AOP: aspect> for declaration.
10. After return notification (after return advice): The Notification executed after a connection point is completed normally, excluding the exception thrown. In applicationcontext, use the <after-returning> element in <AOP: aspect> for declaration.
11. Surround notification (around advice): notifications that enclose 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 method is called, or you can choose not to execute. In applicationcontext, use the <AOP: Around> element in <AOP: aspect> for declaration.

12. notification after an exception is thrown (after throwing advice): Notification executed when the method throws an exception and exits. In applicationcontext, use the <AOP: After-throwing> element in <AOP: aspect> for declaration.

Currently, spring2.0 only supports using method calls as the join point ).

Spring defines the entry point Syntax: excution (modifiers-pattern? Ret-type-pattern declaring-type-pattern? Name-pattern (param-pattern) throws-pattern ?)

Except ret-type-pattern (return type mode) and name-pattern (param-pattern), other modes are optional. The return type mode determines that the return type of a method must match a connection point (that is, a method) in sequence ). The most frequently used return type mode is *, which indicates matching any return type. If you specify the return type, such as string, only the connection points (methods) of the string type can be returned ). The name pattern matches the method name. You can use the * wildcard to match all method names. In parameter mode, () indicates that the method that does not accept any parameters is matched, and (..) The method that matches any number of parameters. Mode (*) indicates a method that matches any type of parameters. Mode (*, string) indicates matching: the first is of any parameter type, and the second must be of the string type.

Modifiers-pattern: Operation permission of the Method

Ret-type-pattern: Return Value

Declaring-type-pattern: package where the method is located

Name-pattern: Method Name

Parm-pattern: Parameter Name

Throws-pattern: exception

The following is an example of defining a start point:

. Execution of any public method:

Excution (Public **(..))

. Execute any method starting with set:

Excution (* Set *(..))

. Execute 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 the service package or sub-package:

Excution (* COM. XYZ. Service ..*.*(..))

------------------------------------------------------- Gorgeous separation line ----------------------------------------------

 

In the spring configuration file, all the aspect and notification devices must be configured in the <AOP: config> label. An applicationcontext can contain multiple <AOP: config> and one <AOP: config> can contain pointcut, Advisor, and aspect elements (note that this sequence is required ).

1. Declare a plane

<AOP: config>

<AOP: aspect id = "myaspect" ref = "mybean">

.....

</AOP: aspect>

</AOP: config>

<Bean id = "mybean" class = "">

.....

</Bean>

Note: <AOP: aspect> is used to declare the aspect. Backing Bean (supports bean) is referenced by Ref.

2. Declare an entry point

<AOP: config>

<AOP: pointcut id = "mypointcut" expression = "excution (* COM. Service. *. * (..)"/>

</AOP: config>

3. Declare a notification

Spring2.0 supports the advisors concept through the <AOP: advisors> element. In most cases, it will be used 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>

Note: advisors must execute the transactions referenced by advice-ref when executing the start point method.

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.