Because the most recent service item provides an interface with a requirement, all operations must check that the operation of the service is available, so the sense that AOP is particularly suitable for implementation. A small example of completing a study.
About SPRING-AOP principle: http://m.oschina.net/blog/174838 This article is very well written.
Personal feel may be on the line when the configuration file more convenient. So the sample is mainly the configuration file mode
Demo File:
http://download.csdn.net/detail/ruishenh/7261121
Spring configuration file
/idle-service-impl/src/main/resources/spring/app-config.xml
<?xml version= "1.0" encoding= "UTF-8"? ><beansxmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx= " Http://www.springframework.org/schema/tx "xmlns:context=" Http://www.springframework.org/schema/context "xmlns: Util= "Http://www.springframework.org/schema/util" xsi:schemalocation= "Http://www.springframework.org/sche Ma/beanshttp://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/ Aophttp://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/txhttp:// Www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/contexthttp:// Www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/utilhttp:// Www.springframework.org/schema/util/spring-util-3.2.xsd"> <context:component-scanbase-package=" Com.ruishenh.business.impl "/> <aop:aspectj-autoproxy Expose-proxy= "true" proxy-target-class= "true"/> <context:annotation-config/> <!----> <beanid= "Springfactoryutil" class= "Com.ruishenh.utils.SpringFactoryUtil"/> <!----> &L T;importresource= "Aop-advisor.xml"/></beans>
The imported Aop-advisor.xml file
/idle-service-impl/src/main/resources/spring/aop-advisor.xml
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx= " Http://www.springframework.org/schema/tx "xmlns:context=" Http://www.springframework.org/schema/context "xmlns: Util= "Http://www.springframework.org/schema/util" xsi:schemalocation= "http://www.springframework.org/schema/ Beanshttp://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/ Aophttp://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/txhttp:// Www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/contexthttp:// Www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/utilhttp:// Www.springframework.org/schema/util/spring-util-3.1.xsd "> <bean id=" genericadvisor "class=" Com.ruisHenh.aop.aspect.advisor.GenericAdvisor "/> <aop:config> <!--defining facets--<aop:aspect ref=" Generi Cadvisor "order=" 0 "> <!--define connection points--<aop:pointcut id=" businessservice "expression=" Execution (* Com.ruishenh.business.impl. *.*(..))" /> <!--definition front-to-<aop:before method= "before" pointcut-ref= "Businessservice"/> <! --Definition of <aop:around method= "Heartbeat" pointcut-ref= "Businessservice"/> <!--definition after <aop:after method= "after" pointcut-ref= "Businessservice"/> <!--define the normal result enhancement after target processing-- Lt;aop:after-returning method= "afterreturning" pointcut-ref= "businessservice" returning= "obj"/> <!--definition Targe T-handle exception-enhanced--<aop:after-throwing method= "handlerexception" pointcut-ref= "Businessservice" throwing= "E"/> </aop:aspect> </aop:config></beans>
About this class
Com.ruishenh.aop.aspect.advisor.GenericAdvisor the method has
1. before the corresponding Target before running
2. Heartbeat This is a way around
3. after the corresponding target runs
4. afterreturning corresponding results after target processing returns enhanced processing
5. handlerexception Enhanced processing when target is running abnormally
/idle-service-impl/src/main/java/com/ruishenh/aop/aspect/advisor/genericadvisor.java Source Code
Package com.ruishenh.aop.aspect.advisor; Import Org.aspectj.lang.joinpoint;importorg.aspectj.lang.proceedingjoinpoint; Import Com.ruishenh.domain.account.AccountBank; public class Genericadvisor {/** * for the method to be enhanced. Run around processing to check if the heartbeat is normal * @param joinpoint * @return * @throws throwable */Objectheart Beat (Proceedingjoinpoint joinpoint) throws throwable{//if (Checkheartbeat ()) {// } System.out.println ("2joinpoint.signature.name:" +joinpoint.getsignature ( ). GetName ()); Remember to call the running method when the exception does not try-catch, to thrwos out, otherwise the transaction layer can not be effective, or enhance the exception handling can not be effective objectobj=joinpoint.proceed (); The participation number below can be changed. However, the number of types and methods should be consistent with the original, otherwise the original method can not run//Objectobj=joinpoint.proceed (Joinpoint.getargs ()); For the returned object, it is possible to do some processing on some of the values of the returned parameters,//To be able to pass the Org.springframework.beans.BeanUtils.copyPrOperties assigns some values to if (obj==null) {returnnew accountbank (); } returnobj; }/** * For methods to be enhanced, run * @param joinpoint connection point information before method */Voidbefore (Joinpoint JOINP oint) {object[] Objs=joinpoint.getargs (); System.out.println ("1OBJS:" +objs[0]); System.out.println ("1joinPoint:" +joinpoint); }; /** * For methods to be enhanced, run after method * @param joinpoint connection point information */Voidafter (Joinpoint joinpoint) { Object[] Objs=joinpoint.getargs (); System.out.println ("4OBJS:" +objs[0]); System.out.println ("4joinPoint:" +joinpoint); }; /** * For the method to be added, the method returns the result. Record or analyze the results * method * @param The result of obj target run/voidafterreturning (Object obj) { System.out.println ("3obj:" +obj); }; /** * For methods to be enhanced, after the method throws an exception. Enhanced handling of exceptions, such as recording exceptions. or according to the anomaly analysis data. * @param e Target exception thrown after run */voidhandlerexception (Throwable e) { System.out.println ("Handingexception ..."); E.printstacktrace (); } booleancheckheartbeat () {returntrue; }}
JUnit Test
/idle-service-impl/src/test/java/com/ruishenh/business/impl/account/accountserviceimpltest.java
Package com.ruishenh.business.impl.account; Import Org.junit.before;import Org.junit.test;import org.junit.runner.RunWith; Importorg.springframework.test.context.ContextConfiguration; Importorg.springframework.test.context.junit4.SpringJUnit4ClassRunner; Importcom.ruishenh.domain.account.accountbank;importcom.ruishenh.domain.account.accountbankparam;import Com.ruishenh.utils.SpringFactoryUtil; @RunWith (Springjunit4classrunner.class) @ContextConfiguration (locations={"Classpath:/spring/app-config.xml"}) public class Accountserviceimpltest {@Before publicvoid setUp () throws Exception {} @T EST publicvoid Teststorein () throws Exception {accountserviceimplimpl= Springfactoryutil.getbea N (accountserviceimpl.class); accountbankparamparam= new Accountbankparam (); Param.setid (100); Accountbankab=impl.storein (param); System.out.println (Ab.tostring ()); } }
Post-run output results:
1objs:[email Protected][id=100,name=<null>,account=<null>]
1joinpoint:execution (AccountBankcom.ruishenh.business.impl.account.AccountServiceImpl.storeIn (Accountbankparam ))
2joinpoint.signature.name:storein
==============todo=====================
4objs:[email Protected][id=100,name=<null>,account=<null>]
4joinpoint:execution (AccountBankcom.ruishenh.business.impl.account.AccountServiceImpl.storeIn (Accountbankparam ))
3obj:[email Protected][id=0,name=<null>,account=<null>]
[Email protected] [Id=0,name=<null>,account=<null>]
Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.
Spring AOP Small Example Demo