First define
A slice class
@Aspect @component
Use the above annotations to indicate that the class is a slice class
And customize two methods in the Slice class, respectively, after executing the
public void before (Joinpoint jp) { ... } public void after (Joinpoint jp) { ... }
Inject the class in the spring configuration file
Then configure the slice program with AOP
<aop:config> <aop:pointcut id= "Beforemethod" expression= "execution (public * com. democlass.* (..)) " /> <aop:aspect id= "Myaspect" ref= "Acpectinterceptor" > <aop:pointcut id= "Aftermethod" expression= "Execution (public * com). democlass.* (..)) " /> <aop:before method= "before" pointcut-ref= "Beforemethod"/> <aop:after-returning method= " After "pointcut-ref=" Aftermethod "/> </aop:aspect> </aop:config>
Slice interception Handler
Package Com.tree.common;import Org.aspectj.lang.joinpoint;import Org.aspectj.lang.annotation.aspect;import org.springframework.stereotype.Component; @Aspect @componentpublic class Myaspectinterceptor {public void before ( Joinpoint JP) {object[] args = Jp.getargs (); for (int i=0;i<args.length;i++) {System.out.println ("before:" +args[i]. GetClass (). GetName ());} System.out.println ("I was executed in the tangent class method [ex]");} public void after (Joinpoint JP) {object[] args = Jp.getargs (), for (int i=0;i<args.length;i++) {System.out.println (" After: "+args[i].getclass (). GetName ());} System.out.println ("I was executed in the tangent class method [after]");}}
Slice Program
Package Com.tree.demo.aop;import Org.springframework.stereotype.Service; @Servicepublic class Aspectprocessor { Public String aspectbizprocess () {return ' {result: I am a slice handler} ';}}
Action
Package Com.tree.aop.action;import Org.apache.commons.logging.log;import org.apache.commons.logging.LogFactory; Import Org.springframework.stereotype.controller;import org.springframework.web.bind.annotation.RequestMapping; Import Org.springframework.web.bind.annotation.responsebody;import Com.tree.common.constants;import Com.tree.demo.aop.AspectProcessor, @Controller @requestmapping (value= "/AOP") public class Testaopaction {private Static log log = Logfactory.getlog (constants.logfile);p rivate aspectprocessor ap;public void Setap (Aspectprocessor ap) { This.ap = AP;} @RequestMapping (value= "/test", produces = "Application/json") @ResponseBodypublic String Testaop () {Log.info (" Enter Aoptest Action "); return ap.aspectbizprocess ();}}
Configuration file
<beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:context= "http://www.springframework.org/ Schema/context "xmlns:aop=" HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP "xmlns:xsi=" http://www.w3.org/2001/ Xmlschema-instance "xsi:schemalocation=" http://www.springframework.org/schema/beanshttp:// www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp:// www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/aophttp:// Www.springframework.org/schema/aop/spring-aop-3.0.xsd "default-autowire=" ByName "><aop:config> <AOP: Pointcut id= "Beforemethod" expression= "Execution (public * com.tree.demo.aop.aspectprocessor.* (..))"/ > <aop:aspect id= "myaspectprocessor" ref= "Myaspect" > <aop:pointcut id= "Aftermethod" expression= "Execution (public * com.tree.demo.aop.aspectprocessor.* (..))"/> <aop:before meth OD= "Before" pointcut-ref= "Beforemethod"/> <aop:after-returning method= "after" pointcut-ref= "AfterMethod"/& Gt </aop:aspect> </aop:config></beans>
Start the Tomcat server and access the action
Http://localhost:8080/demo.web/aop/test.do
You can see that the cut-off program has been executed.
How Spring's slice programming implements the injection plane program