Spring of JAVA framework [Spring things]

Source: Internet
Author: User

Spring provides two types of transaction management: programmatic and declarative. Programming, flexible, but with a large amount of code, there are more repeated code; declarative is more flexible than programming. The main programming method is transactionTemplate. Some commit, rollback, and a series of transaction object definitions are omitted, which need to be injected into the transaction management object. Declarative: mainly uses TransactionProxyFactoryBean and dynamic proxy around Poxy, automatically submits and rolls back transactions. A holistic view of spring transactions is centered on two core PlatformTransactionManager and TransactionStatus. The following is a declarative explanation. Spring declarative statements manage transactions in proxy mode. The Service object we use in Action is actually the proxy object instance, not the Service object instance we wrote. Since there are two different objects, why can we use proxy objects like using Service objects in Action? To illustrate the problem, assume that there is a Service class called AService, and its Spring transaction proxy class is AProxyService. AService implements an IAService interface (here there is an interface IAService, to illustrate the interface proxy method ).

There are two Spring transaction proxy Methods: one is proxy-like and the other is interface proxy. You can specify the proxy method in the Spring configuration file. Currently, all proxies are used. // The following configuration is specified as a class proxy

True

1. Proxy-like Mode

The class proxy method is implemented through inheritance. The following uses pseudocode to describe it.

Interface IAService {
Public void save (ValueObject object );

}

Class AService implements IAService {
Public void save (ValueObject object ){...}

}

// Proxy class automatically generated by Spring.

Class AProxyService extends AService {
Public void save (ValueObject object ){
Try {
Code for starting a transaction;
Super. save (object );
Code for submitting a transaction;
} Catch (Exception e ){
Code for rolling back a transaction;
}
}

}

Call code in Action:

AService a = (AService) getBean ("aProxyServiceBeanName ");

GetBean ("aProxyServiceBeanName") obtains an AProxyService instance. Because AProxyService is a subclass of AService, it can be forcibly converted to AService. When we call a. save (object) Later, we call the AProxyService. save () method, which involves transaction processing. Spring implements transaction management in this way. // The following line of code has the same effect.

IAService a = (IAService) getBean ("aProxyServiceBeanName ");

Spring implements the class proxy mode through CGLib.


2. Interface proxy

The interface proxy method is implemented by implementing interfaces and referencing class instances. Therefore, there must be an interface IAService, which is not required by the class proxy method.

// Proxy class automatically generated by Spring.

Class AProxyService implements IAService {
Private AService aService;
Public void setAService (AService aService ){
This. aService = aService;
}
Public void save (ValueObject object ){
Try {
Code for starting a transaction;
AService. save (object); // note that this line of code is different from the above.
Code for submitting a transaction;
} Catch (Exception e ){
Code for rolling back a transaction;
}
}

}

Call code in Action:

AService a = (AService) getBean ("aProxyServiceBeanName ");

The above code will report the ClassCastException error, because getBean ("aProxyServiceBeanName") obtains the AProxyService instance, and the AProxyService instance cannot be converted to the AService type, although both implement the same interface, there is no inheritance relationship between them. Like ArrayList and aggregate List, they can be converted to List, but they cannot be converted to each other. Therefore, you must use the following code:

IAService a = (IAService) getBean ("aProxyServiceBeanName"); // you need to convert it to the interface type.

Calling the. save () method is actually calling the AProxyService. save () method.

Spring uses Java Dynamic proxy to implement interface proxy

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.