AOP Application Instance - Spring Transaction and Its AOP Framework

xiaoxiao2021-04-07  381

Successful appealing in the Spring framework is the management of container transactions, providing a lightweight container transaction, targeted object is a normal Java class, use Spring transaction management, you can follow some of your business The method is included in its transaction management, which avoids the cumbersome work in the process of dealing with the transaction. At the same time, these are also attractive in the EJB2.x specification, which is good in Spring. Although in the container The transaction management, Spring is not available, but for a general web program, it is not necessary to use EJB for those functions. However, the recent embedded EJB container of JBoss can also do smaller, It is also one of the options in the open source. No matter how technology is developing, there are currently, let's first study the method of AOP implementation.

In fact, transaction processing in Spring is implemented by AOP ideas. Spring AOP is very different from Aspect J and JBoss. First, the user who uses the Spring AOP framework is that Spring Ao is a method for the method. Attachments, while others also provide support. Spectative Spring AOP inside, it is not difficult, for interfaces, using the proxy provided by the Java internal class; for those who do not implement interfaces The class uses the CGLIB library, dynamically creates a subclass to implement.

Four processes in Spring AOP are available: Around, Before, After, Introduction. As the names,

1) Around is a method for a specific entry point (for example, there is a OrderBook method, the type of around is the internal call of this method, which is called through Java's metadata, and calls through Method.Ivoke at runtime. Have a return value, termination when an accident occurs. Remember that the return value is returned.);

2) Before calling before the method calls (call before the OrderBook method, but there is no return value, while usually, it will continue to run the next method. Remember that the point is not returned);

3) After and Before just opposite, there is nothing special place.

4) Introduction is a more special, but the function is more powerful. For example, there is a Book object, Computer object, and dozens of such business objects, now you want to join every other object. A record last modified time. But you don't want to make changes to each class, because it is too much trouble, and more importantly, destroy the integrity of the object, maybe you don't need this time after you will not need this time. ... What should I do this? Spring AOP provides you with this idea to implement this kind of thinking, that is, Introduction.IntRoduction can dynamically add some methods to you, so you can force these objects, The action of inserting time data, deeper insider is VTABLE thinking in the C virtual function). But this kind of dynamic is considering with performance as a price, before use, here we talk about technology, so think he is required of.

Ok, now we will take the fourth way to explain the power of Spring AOP:

1) Assumption created a BookService interface and its implementation method (your own business object):

//$ID: bookService.java create: 2005-11-6 by kerluse bennpackage com.osiris.springaop;

Public interface bookservice {public string ORDERComputerMagazine (String BookName); Public String ORDERBOOK (String UserName);}

//$ID: bookServiceImpl.java create: 2005-11-6 by kerluse bennpackage com.osiris.springaop;

public class BookServiceImpl implements BookService {public String OrderBook (String name, String bookName) {// TODO Add your here String result = null codes; result = "Order" bookName "successful"; return result;} public String OrderComputerMagazine (String userName String BookName) {// Todo Add Your Codes Here String Result = NULL; Result = "Order" BookName "Success"; Return Result;}}

2) In fact, you still have a lot of such objects, now we want to add our function to the last modified time in each object, the functions are as follows:

/ /$ID :lance: 2005-11-7 by kerluse Bennpackage com.Iris.springaop.advents.Intruduction;

Import java.util.date;

Public interface iauditable {void setlastmodifiedDate (Date Date); Date getLastModifiedDate ();

3) Because the type of cuts we use is Introduction, Spring AOP provides us with an interface INTRODUCTIONINTERCEPTOR describing this type, so our cut implementation processing, you also need to implement this interface:

/ (Ips): 2005-11-7 by kerluse bennpackage com.Iris.springaop.advent.Intruduction;

Import java.util.date;

Import org.aopalliance.intercept.methodInvocation; import org.springframework.Aop.IntroductionInterceptor;

public class AuditableMixin implements IAuditable, IntroductionInterceptor {private Date lastModifiedDate; public Object invoke (MethodInvocation m) throws Throwable {// TODO Add your codes here if (. implementsInterface (m.getMethod () getDeclaringClass ())) {return m.getMethod ( ) .invoke (this, m.getarguments ()); // invoke introduces} else {return m.proceed (); // delegate other method}}

Public Date getlastModifiedDate () {// Todo add your code here returniful

Public void setlastmodifieddate (date date) {// Todo add your code = date;}

Public Boolean ImplementSinterface (Class CLS) {// Todo Add Your Codes Here Return CLS.IsassignableFrom (Iauditable.Class);}} 4) OK, now the business object BookService class has, you want to add the processing also has iauditable, then Remove the problem of the Spring AOP framework, configure the bean.xml file:

- beans -> false true AuditableAdvisor The above is the configuration file. Now we assume that the business object is as follows, here is a simple test class:

/ (IID: MAINAPP.JAVA CREATED: 2005-11-6 by kerluse bennpackage com.osiris.springaop; import java.util.date;

Import org.springframework.beans.Factory.Beanfactory; import org.springframework.beans.Factory.xml.xmlbeanfactory; import org.springframework.core.io.filesystemResource;

Import com.osiris.springaop.adivers.Intruduction.iauditable;

public class MainApp {/ ** * @param args * @author Kerluse Benn * / public static void main (String [] args) throws Exception {// TODO Add your codes here BeanFactory factory = new XmlBeanFactory (new FileSystemResource ( "bean. xml ")); BookService bookService = (BookService) factory.getBean (" BookService "); IAuditable auditable = (IAuditable) bookService; System.out.print (bookService.OrderBook (" Kerluse Benn "," Professional C # ")); Auditable.set (New Date ()); System.out.Println ("Order Time is" Auditable.getlastModifiedDate ()); Thread.Sleep (10000); System.out.Print (BookService.orderbook ("Kerluse Benn" "Expert J2EE One-on-One")); Auditable.SetLastModifiedDate (new date ()); system.out.println ("Order Time is" Auditable.GetlastModifiedDate ());}}

Output Result: Order Professional C # Successfully Order Time for Mon Nov 07 11:35:20 CST 2005 Ordering Expert J2EE One-ONE Successfully Ordering Time for Mon Nov 07 11:35:30 CST 2005

See the black body above:

Iauditable auditable = (ipiuditable) bookservice;

Since the BookService object has actually implemented the IAuditable interface, the implementation is implemented via the Spring AOP's Introduction, so when running (familiar with the C VTable model can think in the big brain) can be converted, we can add your own interface method. .

Ok, about Spring AOP is introduced, other related content can be referred to the corresponding book, the purpose of this article is mainly to introduce the power of AOP ideas. Specific related applications include user operation verification, etc. Wait.

转载请注明原文地址:https://www.9cbs.com/read-132535.html

New Post(0)