Configuration file
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"xsi:schemalocation= "Http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd/HTTP Www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd " > <!--activates component scanning to automatically scan components configured with annotations under package CN.YSH.STUDIO.SPRING.AOP and its sub-packages - <Context:component-scanBase-package= "Com.zhiguoguo.service,aspect"/> <!--activating the automatic proxy feature - <Aop:aspectj-autoproxyProxy-target-class= "true"/></Beans>
Service class:
Package Com.zhiguoguo.service; Import org.springframework.stereotype.Component; @Component Public class HelloService { public String SayHello () { System.out.println ("—————— method execution —————— "); // throw new RuntimeException ("haha"); return "Hello world!" ; }}
Facet AOP:
PackageCom.zhiguoguo.aspect;ImportOrg.aspectj.lang.ProceedingJoinPoint;Importorg.aspectj.lang.annotation.*;Importorg.springframework.stereotype.Component;/*** Define Facets*/@Component @aspect Public classhelloserviceaspect {@Before ("Execution (* com.zhiguoguo.service.helloservice.* (..))") Public voidAuthorith () {System.out.println ("Impersonation for permission checking. "); } @After ("Execution (* com.zhiguoguo.service.helloservice.* (..))") Public voidrelease () {System.out.println ("Releasing resources after simulation method ends ..."); System.out.println (); } @AfterReturning (Returning= "obj", pointcut= "Execution (* com.zhiguoguo.service.helloservice.* (..))") Public voidlog (Object obj) {System.out.println ("Simulated target method return value:" +obj); System.out.println ("Analog logging Function ..."); System.out.println (); } @AfterThrowing (Throwing= "Ex", pointcut= "Execution (* com.zhiguoguo.service.helloservice.* (..))") Public voiddorecoveractions (Throwable ex) {System.out.println ("Exception thrown in the target method:" +ex); System.out.println ("Enhanced processing after impersonation throws an exception ..."); }//@Around ("Execution (* com.zhiguoguo.service.helloservice.* (..))")//here, first comment out . PublicObject Processtx (proceedingjoinpoint JP)throwsjava.lang.Throwable {System.out.println ("Simulate the beginning of things before executing the target method ..."); //executes the target method and saves the return value after the target method executesObject rvt =jp.proceed (); //the tangent method here can pass a parameter to him if it has parameters.//Object rvt = jp.proceed (new string[]{"changed parameter"});System.out.println ("Simulate the end of things before executing the target method ..."); System.out.println (); //after such a surround, you can enhance the return value returnRVT + "What's new"; }}
Main function:
Packagemain;ImportCom.zhiguoguo.service.HelloService;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classHelloApp { Public Static voidMain (string[] args) {ApplicationContext context=NewClasspathxmlapplicationcontext ("Spring-config.xml"); HelloService HelloService= Context.getbean (HelloService.class); Helloservice.sayhello (); }}
A simple example of Spring AOP