The inside of spring transaction processing and its AOP framework

Source: Internet
Author: User
Tags aop jboss

One of the things that is successful in the spring framework is the management of container transactions, which provides a lightweight container transaction for objects that are ordinary Java classes, and with spring transaction management, you can incorporate some of the relevant methods into their transaction management in accordance with your business. This avoids the tedious work of programmers in the process of dealing with transactions. These are also the attractions of the ejb2.x specification, which are well provided in spring. Although it is not available in spring in cross container transaction management, it is not necessary for ordinary Web applications to have the ability to With EJB. However, the recent JBoss embedded EJB container can also be made smaller, but also one of the options in open source. No matter how technology develops, at present, we first study the method of AOP implementation.

In fact, the transaction processing in spring is implemented through AOP, and spring AOP is very different from Aspect J and JBoss, and first of all, users of the spring AOP framework have to remember that spring AOP is about implementation at the methodological level, The other two also provide support for fields. When it comes to the insider of Spring AOP, it's not difficult to use the proxy in Java internal classes for classes with interfaces, and for classes that do not implement interfaces, the Cglib library is used to dynamically create a subclass to implement.

In spring AOP, 4 types of processing are available: Around,before,after,introduction.

1 around is a method for a specific pointcut (for example, now there is a Orderbook method, the around type is the internal call on this method, is through the Java metadata, Called at run time by Method.invoke, with a return value that terminates when an accident occurs. Remember that the return value.

2) before is called before the method call (called before the Orderbook method, but there is no return value, and the next method continues to run in the usual unexpected circumstances. Remember that there is no return value);

3 after and before just the opposite, there is no special place.

4 Introduction is a more specific, but more powerful type of cut. For example (you now have book objects, computer objects, and dozens of such business objects, Now you want to include a record of the last modified time in each of these objects. But you do not want to make changes to each class, because too cumbersome, but also more important point, destroy the integrity of the object, you may not need this time data again ... What do we do then? Spring AOP provides a way for you to specifically implement this idea, which is that introduction.introduction can dynamically add some methods to you, so that you can cast these objects at run time, insert the time data, and the deeper inside is the c+ + vtable idea in virtual function. But this dynamic is at the cost of performance, before use to carefully consider, here we talk about technology, so we think he is necessary.

Okay, now let's take the fourth to illustrate the power of spring AOP:

1 Suppose you create a Bookservice interface and its implementation (your own business object):

//?$ID:BookService.java Created:2005-11-6 by Kerluse Benn
package com.osiris.springaop;
public interface BookService {
  public String OrderComputerMagazine(String userName,String bookName);
  public String OrderBook(String userName,String bookName);
}
//?$ID:BookServiceImpl.java Created:2005-11-6 by Kerluse Benn
package com.osiris.springaop;
public class BookServiceImpl implements BookService{
  public String OrderBook(String name,String bookName) {
   // TODO Add your codes here
   String result=null;
   result="订购"+bookName+"成功";
   return result;
  }
  public String OrderComputerMagazine(String userName, String bookName) {
   // TODO Add your codes here
   String result=null;
   result="订购"+bookName+"成功";
   return result;
  }
}

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.