First define
A slice class
@Aspect
@Component
Use the above annotation to indicate that this class is a slice class
And customize two methods in this slice class, respectively, before execution
public void before (Joinpoint jp) {
...
}
public void after (Joinpoint jp) {
...
}
This class is injected into 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 intercept Handler
Package Com.tree.common;
Import Org.aspectj.lang.JoinPoint;
Import Org.aspectj.lang.annotation.Aspect;
Import org.springframework.stereotype.Component;
@Aspect
@Component Public
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 executing" in the cut-cut class method [before]);
}
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 executing" in the cut-cut class method [after]);
}
Slice Program
Package COM.TREE.DEMO.AOP;
Import Org.springframework.stereotype.Service;
@Service public
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);
Private Aspectprocessor ap;
public void Setap (Aspectprocessor ap) {
This.ap = ap;
}
@RequestMapping (value= "/test", produces = "Application/json")
@ResponseBody public
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/beans http:// Www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http:// Www.springframework.org/schema/context/spring-context-3.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http:// Www.springframework.org/schema/aop/spring-aop-3.0.xsd "default-autowire=" byname "> <aop:config> <AOP :p ointcut 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 method= "Before" pointcut-ref= "Beforemethod"/> <aop:after-returning method= "after" poi ntcut-ref= "Aftermethod"/> </aop:aspect> </aop:config> </beans>
Start Tomcat server, Access action
Http://localhost:8080/demo.web/aop/test.do
You can see that the slice program has been executed.