Spring's AOP and transactions

Source: Internet
Author: User
Tags aop exception handling object model

1. Load additional external configuration files or properties files in spring via propertyplaceholderconfigurer:

In many Java EE projects, Spring's role is very important and is a lightweight container for managing other modules and components, and spring often needs to manage struts, Ibatis, hibernate, etc. The configuration files for these open source frameworks are managed in spring through spring's propertyplaceholderconfigurer loading, plus database connection information, Jndi connection Information properties files can also be managed by propertyplaceholderconfigurer loading into spring. Use the following:

(1). Load additional files into spring via propertyplaceholderconfigurer:

Add the following configuration to the spring configuration file:

[XHTML] View plain copy <bean class= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >          <property name= "Locations" > <value>classpath: File name to load </value> ... </property> </bean>

(2). After the configuration or property files that are loaded in (1) are loaded into spring, if you need to use some information from the load-in configuration or data file at run time, such as using database connection information or Jndi connection information, you can use the syntax of the type El expression to refer to it. For example:

[XHTML] View plain copy <bean id= "DataSource" destroy-method= "Close" class= "Org.apache.common.dbcp.BasicDataSource "> <!--assume that the database connection information is written in an external property file that has been loaded by spring--<property name=" Driverclassname "value=" ${driver} "/          > <property name= "url" value= "${url}"/> <property name= "username" value= "${username}"/> <property name= "Password" value= "${password}"/> </bean>

Note: You can also use the <context:property-placeholderlocation= "classpath: filename to load"/>

2. Dynamic Agent for Java:

The underlying implementation of spring's aspect-oriented programming (AOP) is dynamic proxies, so you must understand dynamic proxies before you can learn to face-slicing programming.

Dynamic proxies are widely used in Java, and dynamic proxies are one of the classic design patterns that are very common in design patterns in 23. The principle of dynamic proxy is that when a target object or its method is called, the system does not return the target object directly, but instead returns a proxy object, which accesses the target object or the method of the target object through the proxy object.

The simple principle of dynamic proxy is as follows:

The client caller, the proxy object, and the target object being called.

When a client invokes a proxy object, the proxy object delegates the target object to invoke its business method.

Dynamic agent is divided into two kinds, the dynamic agent for the interface and the dynamic agent for the ordinary class, the dynamic agent in Java is the dynamic agent of the real interface, Cglib is the dynamic agent for the ordinary class, the target Java EE's dependency package and the Spring jar package already contain the Cglib related jar package. Therefore, the agent can also be dynamic proxy for the ordinary class.

(1). Java for interface Dynamic proxy:

Dynamic proxies in Java can only be dynamic proxies for interfaces, so the target object must implement an interface that the proxy object implements for all interfaces of the target object. The work flow is as follows:

A. Dynamic proxy class Authoring:

Note: Dynamic proxies must implement the Invocationhandler interface while implementing the following methods:

[Java] View plain Copy object Invoke (OBJECTM proxy instance, method instance of interface methods invoked on the method proxy instance, object[] An array of objects that pass the parameter values of the methods call on the proxy instance);

Installs the documentation description of the JDK, which is used to pass the proxy instance, identify the Java.lang.reflect.Method object that invokes the method, and an array of type object containing the arguments. The calling handler handles the encoded method call in an appropriate manner, and the result it returns is returned as the result of the method call on the proxy instance.

B. Create a proxy object:

[Java] View plain copy proxy.newproxyinstance (class loader, class<?>[] interface array, callback proxy object (typically this))

When the target object method is called, the proxy object of the target object is created by the method, and the proxy object automatically calls its Invoke method to invoke the target object and returns the result of the call.

(2). Cglib for normal Java class dynamic proxies:

Cglib the target class must implement an interface when creating a dynamic proxy, its workflow is as follows:

A. Dynamic proxy class Authoring:

[Java] View plain copy enhancer enhancer = new enhancer ();   Sets the parent class of the target class to its own Enhancer.setsuperclass (target class object. GetClass ()); Sets the callback object to be the dynamic proxy object itself Enhancer.setcallback (this);

B. Implement the Methodinterceptor interface:

Implement the following methods:

[Java] View plain copy object Intercept (OBJECTM proxy instance, method instance of the interface methods invoked on the method proxy instance, object[] An array of objects that pass the parameter values of the methods call on the proxy instance, Methodproxy method Proxy instance);

Note: Cglib is not only available for class dynamic proxies, but also for method dynamic proxies.

3. Basic concepts for tangent-plane programming (AOP):

Take an example of an ordinary Java method

[Java] View plain copy public return type method name (parameter list) {--> wrapping notification method before handling code--Pre-notification try{method specific implementation (method body) ...          .... Method after processing code--post-notification}catch (Exception type E) {Exception handling ......--> exception notification}finally{last processing agent ...--&     Gt Final Notification}}

A. Crosscutting concerns: such as the location of the above 5 notifications, in a Java object, these objects with similar common processing logic, such as permission validation, transaction processing, logging, and other processing logic, are called crosscutting concerns, object-oriented programming (OOP) The focus is on the vertical abstraction of real-world things into programming object models. The focus of aspect-oriented programming (AOP) is landscape, which abstracts the programming object model with similar processing logic to form facets, while the processing logic in programming objects is crosscutting concerns.

B. Facets (Aspect): Abstract cross-cutting concerns into slices, similar to classes, with different concerns, classes are abstractions of the nature of things, and facets are abstractions of crosscutting concerns.

C. Connection point (Joinpoint): The point that is intercepted, in spring, because spring supports only the connection point of the method type, that is, the method being intercepted. As in the above example method.

D. Pointcut (Pointcut): Refers to the definition of a connection point interception, is a collection of connection points, a series of intercepted methods.

E. Notification (Advice): Refers to what is to be done after interception to the connection point, that is, the logical processing after interception. Common permissions validation, transaction processing, logging, and so on are defined and completed in the notification.

F. Target object: The target object of the agent, which is the object being intercepted. As in the example above, the object in which the method resides.

G. Weaving (Weave): Refers to the process of applying a slice to the target object and causing the proxy object to be created.

H. Introduction (Introduction): Without modifying the code, introduce methods and fields that can be dynamically added to the class at run time.

1. Spring supports section-oriented programming (AOP)-dependent packages:

Spring extracts the following 3 packages from the directory:

Lib/aspectj/aspectjweaver.jar

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.