1. Several concepts involved
Slice class, cut object, entry point, cut-in time, cut-in content; (the name of his own life, good understanding point)
2, look at the configuration file
<?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"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsdhttp//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOPhttp//www.springframework.org/schema/aop/spring-aop-2.0.xsdhttp//Www.springframework.org/schema/txhttp//www.springframework.org/schema/tx/spring-tx-2.0.xsd "><bean id= "Audience"class= "Com.spring.test.aop.Audience"/> <bean id= "Sax"class= "Com.spring.test.setter.Saxophone"/> <bean id= "Kenny"class= "Com.spring.test.setter.Instrumentalist" > <property name= "song" Value= "Jingle Bells"/> <prop Erty name= "age" value= "/> <property name=" instrument "ref=" sax "/> </bean> <aop:con Fig proxy-target-class= "true" > <aop:aspect ref= "Audience" > <aop:pointcut id= "Performance" expression= "Executio N (* com.spring.test.setter.instrumentalist.* (..)) " /> <!--<aop:pointcut id= "Performance1" expression= "Execution (* COM.SPRING.TEST.ACTION1.PERFORMER.PERFO RM (..)) " />--> <aop:before pointcut-ref= "Performance" method= "Takeseats"/> <aop:be Fore pointcut-ref= "Performance" method= "Turnoffcellphones"/> <aop:after-returning pointcut-ref= "Performan Ce "method=" applaud "/> <aop:after-throwing pointcut-ref=" Performance "method=" Demandrefund "/> </aop:aspect> </aop:config></beans>
AOP configuration file
3, need to introduce the JAR package
4. Code
Package Com.spring.test.action1; Public Interface Performer { void perform ();}
PackageCom.spring.test.setter;ImportCom.spring.test.action1.Performer;/*** Being cut-off class, (usually business class) *@authorAdministrator **/ Public classInstrumentalistImplementsperformer{PrivateString Song; Private intAge ; Privateinstrument instrument; Public intGetage () {returnAge ; } Public voidSetage (intAge ) { This. Age =Age ; } PublicString Getsong () {returnSong; } Public voidSetsong (String song) { This. Song =Song; } PublicInstrument getinstrument () {returninstrument; } Public voidSetinstrument (instrument instrument) { This. Instrument =instrument; } Publicinstrumentalist () {} PublicInstrumentalist (String song,intage,instrument instrument) { This. Song =Song; This. Age =Age ; This. Instrument =instrument; } Public voidperform () {System.out.println ("Instrumentalist Age:" +Age ); System.out.print ("Playing" +song+ ":"); Instrument.play (); } Public voidsay () {System.out.println ("This is saying!!!!!"); }}
Package Com.spring.test.setter; Public Interface Instrument { publicvoid play ();}
Package Com.spring.test.setter; Public class Implements Instrument { public saxophone () {} publicvoid play () { System.out.println ("Toot Toot Toot");} }
PackageCOM.SPRING.TEST.AOP;/*** Slice class *@authorAdministrator **/ Public classAudience { Public voidtakeseats () {System.out.println ("The audience is taking their seats."); } Public voidTurnoffcellphones () {System.out.println ("The audience is turning off their cellphones"); } Public voidapplaud () {System.out.println ("Clap Clap Clap"); } Public voidDemandrefund () {System.out.println ("Boo! We want Money Back "); }}
PackageCom.spring.test.setter;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classTest { Public Static voidMain (string[] args) {ApplicationContext ctx=NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); Instrumentalist performer= (instrumentalist) Ctx.getbean ("Kenny"); Performer.perform (); Performer.say (); }}
5. For the pointcut expression, see:
Http://www.cnblogs.com/yanjunwu/p/3996570.html
Advantages of cutting programming: It is advantageous to system maintenance, upgrade, optimization and transformation, and facilitates the separation of business logic;
Reference: www.cnblogs.com/xing901022/
Implementation of Spring AOP oriented slicing programming