Compared to the spring interceptor, there are generally two, the first is the use of Proxyfactorybean, the second is the use of beannameautoproxycreator. This paper introduces the simple use examples of these two interceptors, then compares and analyzes the advantages and disadvantages. using Proxyfactorybean as the Interceptor trilogy
1. Define the business interface and implementation class first (no interfaces or inheriting classes are implemented or extended)
Business interface Public
interface SayHello
{public
void Sayhell ();
}
One of the business interface implementation Classes Public
class Saychinesehelloimpl implements SayHello
{
/* (non-javadoc)
* @see test. Sayhello#sayhell ()
*/public
void Sayhell ()
{
printutil.print ("Hello!")
;
}
Business implementation Interface Class Two public
class Sayenglishhello implements SayHello
{
/* (non-javadoc)
* @see test. Sayhello#sayhell ()
*/public
void Sayhell ()
{
printutil.print ("Hello!");}
}
2. Define interceptors (implement Methodinterceptor interface)
Define log interceptors public
class Loginterceptor Implementsmethodinterceptor
{
/* (non-javadoc)
* @see Org.aopalliance.intercept.methodinterceptor#invoke (org.aopalliance.intercept.MethodInvocation)
*
/ Public Object Invoke (methodinvocation invocation) throwsthrowable
{
dobusiness (invocation);
return invocation.proceed ();
}
Private Object dobusiness (methodinvocation invocation) {
System.out.print ("I ' m do nothing except record log .....");
return null;
}
}
3.spring.xml Configuration
<bean id= "Saychinesehello" class= "test. Saychinesehelloimpl "/>
<bean id=" Sayenglishhello "class=" test. Sayenglishhello "/>
<bean id=" Loginterceptor "class=" interceptor. Loginterceptor "/>
<bean id=" Sayhelloproxy "class=" Org.springframework.aop.framework.ProxyFactoryBean " >
<property name= "proxyinterfaces" >
<!--the full class name of the interface you want to intercept and
<value>test. sayhello</value>
</property>
<property name= "Interceptornames" >
<list>
<!--a list of interceptors and business classes, the business class must be last, and only one business class can be intercepted--
<value>logInterceptor</value>
<value >sayEnglishHello</value>
</list>
</property>
</bean>
Okay, so far, everything is ready to start using:
public class Interceptortest
{
private applicationcontext context=null;
@Before public
void init () {
context=newclasspathxmlapplicationcontext ("Spring.xml");
}
@Test public
void Testinterceptor () {
System.out.println ("Sayenglishhello whitout proxy!");
SayHello sayenglishhello= (SayHello) Context.getbean ("Sayenglishhello");
Sayenglishhello.sayhell ();
System.out.println ("Saychinesehello whitout proxy!");
SayHello saychinesehello= (SayHello) Context.getbean ("Saychinesehello");
Saychinesehello.sayhell ();
System.out.println ("Sayenglishhello whit proxy!");
SayHello sayhelloproxy= (SayHello) Context.getbean ("Sayhelloproxy");
Sayhelloproxy.sayhell ();
}
}
Output results
Sayenglishhello whitout proxy!
Hello!
Saychinesehello whitout proxy!
Hi there!
Sayenglishhello whit proxy!
I ' m nothing except record log ...
.. Hello!
Beannameautoproxycreator is similar to the above, only the configuration is different:
<bean id= "Saychinesehello" class= "test. Saychinesehelloimpl "/> <bean id=" Sayenglishhello "class=" test. Sayenglishhello "/> <bean id=" loginterceptor "class=" interceptor. Loginterceptor "/> <bean class=" Org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator "id=" Test_proxy "> <property name=" beannames "> <list> <!--List of business class IDs to intercept--<va lue>sayenglishhello</value> <value>sayChineseHello</value> </list> </prop erty> <property name= "Interceptornames" > <list> <!--Interceptor List--<value >logInterceptor</value> </list> </property> </bean>
public class Interceptortest
{
private applicationcontext context=null;
@Before public
void init () {
context=new classpathxmlapplicationcontext ("Spring.xml");
}
@Test public
void Testinterceptor () {
System.out.println ("Sayenglishhello whit Beannameautoproxycreator! ");
SayHello sayenglishhello= (SayHello) Context.getbean ("Sayenglishhello");
Sayenglishhello.sayhell ();
System.out.println ("Saychinesehello whit beannameautoproxycreator!");
SayHello saychinesehello= (SayHello) Context.getbean ("Saychinesehello");
Saychinesehello.sayhell ();
}
}
? then test? View Test Results
Sayenglishhello whit beannameautoproxycreator!
I ' m nothing except record log ...
.. Hello!
Saychinesehello whit beannameautoproxycreator!
I ' m nothing except record log ...
.. How are you doing!
Summary: Both interceptors require three steps: Implement the Business class, implement the Interceptor, and configure the business class as well as the interceptor relationship. If there are a lot of beans that need to be proxied, using Proxyfactorybean will undoubtedly cause a heavy effort to write the spring configuration file because Proxyfactorybean does not support the proxy list. Beannameautoproxycreator considering this, both support multiple interceptors, and support multiple agent beans, notch above, haha
Transaction Management configuration
<?xml version= "1.0" encoding= "UTF-8"?> <! DOCTYPE beans Public "-//spring//dtd bean//en" "Http://www.springframework.org/dtd/spring-beans.dtd" > <beans&
Gt <bean id= "DataSource" class= "Org.apache.commons.dbcp.BasicDataSource" > <property name= "Driver ClassName "> <value>com.mysql.jdbc.Driver</value> </property> <PR Operty name= "url" > <value>jdbc:mysql://localhost:3306/myuser</value> </property&
Gt <property name= "username" > <value>lighter</value> </property> &L T;property name= "Password" > <value>12345</value> </property> </bean&
Gt <bean id= "Sessionfactory" class= "Org.springframework.orm.hibernate3.LocalSessionFactoryBean" > &L T;property name= "DataSource" ref= "DataSource"/> ≪property name= "Hibernateproperties" > <props> <prop key= "Hibernate.dialect" &
Gt Org.hibernate.dialect.MySQLDialect </prop> <prop key= "Hibernate.show_sql" > true</prop> </props> </property> <property name= "Mappingresources" &
Gt
<list> <value>org/mmc/dao/domain/User.hbm.xml</value> </list>
</property> </bean> <!--Configure Hibernate transaction manager-<bean id= "TransactionManager" class= "Org.springframework.orm.hibernate3.HibernateTransactionManager" > <property name= "sessionf Actory "ref=" Sessionfactory "/> </bean> <!--define transaction Interceptor Bean--<bean id=" Transactionin
Terceptor "class=" Org.springframework.transaction.interceptor.TransactionInterceptor "> <property name= "TransactionManager" ref= "TransactionManager"/> <property name= "transactionAttr
Ibutes "> <props> <prop key=" insert* ">PROPAGATION_REQUIRED</prop> <prop key= "find*" >PROPAGATION_REQUIRED,readOnly</prop> <prop key= "*" >pro pagation_required</prop> </props> </property> </bean> <!- -Define beannameautoproxycreator--> <bean class= "Org.springframework.aop.framework.autoproxy.BeanNameAut Oproxycreator "> <property name=" beannames "> <!--all beans whose names end with Dao,service, will be created by the" Bean Post processor "
Transaction agent; In fact, transaction management should be done at the business level, here is just a simple example-<value>*DAO,*Service</value> </property>
<!--The following defines the transaction interceptors required for Beannameautoproxycreator-<property name= "Interceptornames" >
<list> <!--can add other interceptors--<value>transactionInterceptor</value> & Lt;/list> </property> </bean> <!--Take the following example: This time, this bean has a transaction management, can add a similar bean--&G
T <bean id= "Searchuserdao" class= "Org.mmc.dao.impl.SearchUserDAO" > <property name= "sessionfactory" ref= "s Essionfactory "/> </bean> </beans>
ref:http://my.oschina.net/qicheng/blog/168085