Spring AOP and transaction agent mechanism
Spring provides a build factory for Transactionproxyfactorybean as a dynamic agent that produces a dynamic AOP transaction agent for the target bean. The transaction agent automatically manages transaction operations based on the transaction properties that are configured. The transactional agent's transparent management of the transaction relies on a platformtransactionmanager that is globally visible in the ApplicationContext scope. Spring provides two default transaction manager implementations: Datasourcetransactionmanager and Jtatransactionmanager. The former only supports a single JDBC data source, which can support multiple data sources and can do distributed transaction management. Typically, the container provides a JTA-enabled transaction manager implementation, and spring's Jtatransactionmanager automatically detects the presence of TransactionManager or usertransaction through Jndi. You can also specify Jndi settings in the configuration. If you want to implement distributed transaction management without relying on containers, you can implement JOTM with open source transaction manager. The transaction manager must know the location of the managed datasource, and if it is Datasourcetransactionmanager, specify a reference to the JDBC data source directly in the transaction manager configuration, and if it is Jtatransactionmanager, The container is responsible for informing the transaction manager of the data sources that need to be managed. In either case, you need to configure the data source in the DAO that corresponds to the transaction manager.
1. What is AOP.
AOP is the continuation of OOP and is the abbreviation of Aspect oriented programming, meaning aspect-oriented programming. AOP is actually a continuation of the GOF design pattern, and the design pattern pursues the decoupling between the caller and the callee, and AOP can be said to be an implementation of this goal.
2. What is the meaning of facets?
You can perform unified centralized rights management on this slice. Business logic components, however, do not need to be concerned with permissions issues. In other words, we can isolate the problems at different levels in the system through the aspect, and realize the unified and intensive processing. All facets need only focus on the logical implementation within their own domain. This aspect makes the development logic clearer, specialization division more easy to carry out, on the other hand, because of the separation of facets, reduce the coupling, we can be used in different applications to combine the various facets, which makes the code reusability greatly enhanced.
3. AOP Application scope
Authentication Permissions
Caching Cache
Context Passing content delivery
Error handling Errors Handling
Lazy Loading lazily loaded
Debugging commissioning
Logging, tracing, profiling and monitoring record tracking optimization calibration
Performance Optimization Performance optimization
Persistence persistence
Resource Pooling resource Pool
Synchronization synchronization
Transactions transactions
Three. Spring Transaction processing
What can 1.Spring transaction management bring to us?
For traditional transactional processing based on a particular transactional resource, such as JDBC-based database access, spring does not have any effect on it, so we can successfully write and run such code. At the same time, spring also provides some auxiliary classes that we can choose to use, which simplifies the traditional database operation process, saves the workload and improves the coding efficiency to some extent.
Spring shows great value for parameterized transaction management of dependent containers. Spring itself is a container, but spring is much lighter and smaller relative to the EJB container. We can implement container-based transaction management (essentially, spring's transaction management is based on dynamic AOP) through spring without paying any other price.
2. Hibernate in Spring
Applicationcontext.xml
<!--Hibernate sessionfactory--><bean id= "Sessionfactory"
class= "Org.springframework.orm.hibernate.LocalSessionFactoryBean" >
<property name= "DataSource" ><ref local= "DataSource"/></property>
<property name= "Mappingresources" >
<list><!--the Add list of. hbm.xml Files here--
<value>org/mzone/model/Tuser.hbm.xml</value>
<value>org/mzone/model/Article.hbm.xml</value>
</list>
</property>
<property name= "Hibernateproperties" >
<props>
<prop key= "Hibernate.dialect" >net.sf.hibernate.dialect.SybaseDialec</prop>
<prop key= "Hibernate.show_sql" >True</prop>
</props>
</property>
</bean><!--Transaction Manager for a single Hibernate sessionfactory (alternative to JTA)--
<bean id= "TransactionManager"
class= "Org.springframework.orm.hibernate.HibernateTransactionManager" >
<property name= "sessionfactory" ><ref local= "Sessionfactory"/></property>
</bean>
<bean id= "Basetxproxy" lazy-init= "true"
class= "Org.springframework.transaction.interceptor.TransactionProxyFactoryBean" >
<property name= "TransactionManager" ><ref bean= "TransactionManager"/></property>
<property name= "Target" >
<ref local= "Usermanagertarget"/>
</property>
<property name= "Transactionattributes" >
<props>
<prop key= "save*" >PROPAGATION_REQUIRED</prop>
<prop key= "get*" >PROPAGATION_REQUIRED,readOnly</prop>
<prop key= "remove*" >PROPAGATION_REQUIRED</prop>
<prop key= "*" >PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean id= "Usermanagertarget" class= "Org.mzone.service.impl.UserManagerImpl" >
<property name= "Userdao" ><ref local= "Userdao"/></property>
<property name= "Articledao" ><ref local= "Articledao"/></property>
</bean>
Userdao.java Articledao.java
public class Userdaoimpl extends Hibernatedaosupport implements Userdao {
public void Saveuser (Tuser user) {
Gethibernatetemplate (). saveorupdate (user);
}}public class Articledaoimpl extends Hibernatedaosupport implements Articledao {
public void savearticle (article article) {
Gethibernatetemplate (). saveorupdate (article);
}}
Hibernatedaosupport
Implements the association of Hibernatetemplate and Sessionfactory instances. Hibernatetemplate the Hibernate session operation is encapsulated, and the Hibernatetemplate.execute method is the core of a package mechanism, which is interesting to study its implementation mechanism.
With hibernatetemplate we can get rid of the tedious operation of the session instance, start transaction, COMMIT/ROLLBACK TRANSACTION, and chores try/catch/finally each data operation must first. In order to obtain the logical rendering effect in the above code.
Org.mzone.service.impl.UserManagerImpl
public class Usermanagerimpl implements Usermanager {
Private Userdao Userdao;
Private Articledao Articledao;
public void Saveuserandarticle (Tuser user, article article) {
Userdao.saveuser (user);
Articledao.savearticle (article);
}}
Test code
InputStream is = new FileInputStream ("Applicationcontext.xml");
Xmlbeanfactory factory = new Xmlbeanfactory (IS); Usermanager Usermanager =
(Usermanager) Factory.getbean ("Basetxproxy");
user = new Tuser ();
Article = new article ();
User.setusername ("Hellboys_topic 1");
User.setpassword ("12345678_topic 1");
Article.settitle ("Hellboys_topic 1");
Article.setcontent ("Hellboys_topic 1");
Usermanager.saveuserandarticle (user,article);
Attention issues
Usermanager Usermanager =
(Usermanager) Factory.getbean ("Basetxproxy"); Usermanager Usermanager =
(Usermanagerimpl) Ctx.getbean ("Basetxproxy"); java.lang.ClassCastException
The reason is that spring's AOP implementation mechanism, mentioned earlier, is actually based on the dynamic AOP mechanism for transaction management in spring, in order to implement dynamic aop,spring By default Java dynamicproxy is used, but dynamic Proxy requires that the object of its proxy implement an interface that defines the method that prepares the proxy. For Java classes that do not implement any interface, there are other ways in which spring implements this functionality through CGLIB10.
Cglib can modify the class behavior at run time. Because of its powerful and outstanding performance, it is often used as the Java Dynamic Proxy
The implementation basis of the dynamic proxy mode. The Cglib class library is used in spring and hibernate.