Here's an example of a calculator development
1 Setting up the environment-with a good spring AOP development environment
Import the following packages:
2 Building a class of core processing modules
Arithmeticcalculator:
Package Com.jeremy.spring.aspectj;public interface Arithmeticcalculator {int add (int i,int j); int sub (int i,int j); int mu L (int i,int j); int div (int i,int j);}
Arithmeticcalculatorimp
PackageCom.jeremy.spring.AspectJ;Importorg.springframework.stereotype.Component; @Component Public classArithmeticcalculatorimplImplementsArithmeticcalculator {@Override Public intAddintIintj) {intResult =i+J; System.out.println ( This. GetClass () + "" + This. Hashcode ()); returnresult; } @Override Public intSubintIintj) {intResult =i-J; returnresult; } @Override Public intMulintIintj) {intResult =i*J; returnresult; } @Override Public intDivintIintj) {intResult =i/J; returnresult; }}
3 Create a slice class and configure the slice information inside
PackageCom.jeremy.spring.AspectJ;Importjava.util.Arrays;ImportOrg.aspectj.lang.JoinPoint;ImportOrg.aspectj.lang.annotation.After;ImportOrg.aspectj.lang.annotation.Aspect;ImportOrg.aspectj.lang.annotation.Before;Importorg.springframework.stereotype.Component;/** * AOP HelloWorld * 1. Add JAR Package * Com.springsource.net.sf.cglib-2.2.0.jar * com.springsour Ce.org.aopalliance-1.0.0.jar * Com.springsource.org.aspectj.weaver-1.6.8.release.jar * Spring-aspects-4.0.0.release.jar * * 2. Include the AOP namespace in the Spring configuration file. * * 3. Annotation-based approach to using AOP * 3.1 To configure automatically scanned packages in a configuration file: <context:component-scan base-package= "COM.ATGUIGU.SPRING.AOP" ></ context:component-scan> * 3.2 Adding configuration to make ASPJECTJ annotations work: <aop:aspectj-autoproxy></aop:aspectj-autoproxy> * Automatically generates dynamic proxy objects for matching classes. * * 4. Write the Slice class: * 4.1 A generic Java class * 4.2 adds features to be implemented in addition. * * 5. Configure Facets * 5.1 facets must be a bean in the IOC: the actual addition of the @Component annotation * 5.2 Declaration is a facet: add @Aspect * 5.3 Declaration notification: That is, the additional method of adding the function corresponds. * 5.3.1 Pre-notification: @Before ("Execution (public int com.atguigu.spring.aop.arithmeticcalculator.* (int))") * @Before Represents the method body of a method that executes a @Before tag before the target method executes. * @Before is the pointcut expression: * * 6. Accessing connection details in notifications: You can add parameters to the Joinpoint type in the notification method, from which you can access the signature of the method and the parameters of the method. * * 7. @After represents a post-notification: code that executes after the method executes. *///declaring a bean by adding @Aspect annotations is a slice!@Aspect @component Public classloggingaspect {@Before ("Execution (public int com.jeremy.spring.aspectj.arithmeticcalculator.* (int, int))") Public voidBeforemethod (Joinpoint joinpoint) {String methodName=joinpoint.getsignature (). GetName (); Object [] args=Joinpoint.getargs (); System.out.println ("The method" + MethodName + "begins with" +arrays.aslist (args)); } @After ("Execution (* com.jeremy.spring.aspectj.*.* (..))") Public voidAftermethod (Joinpoint joinpoint) {String methodName=joinpoint.getsignature (). GetName (); System.out.println ("The method" + MethodName + "ends"); } }
4 Configuring the Spring XML file
3 annotation-based approaches to using AOP
3.1 Configure the automatically scanned package in the configuration file: <context:component-scan base-package= "COM.ATGUIGU.SPRING.AOP" ></context: Component-scan>
3.2 Adding a configuration that makes ASPJECTJ annotations work: <aop:aspectj-autoproxy></aop:aspectj-autoproxy> automatically generates dynamic proxy objects for matching classes.
<?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:context= "Http://www.springframework.org/schema/context"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-4.0.xsd/HTTP Www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd " > <!--packages that are automatically scanned - <Context:component-scanBase-package= "Com.jeremy.spring.AspectJ"></Context:component-scan> <!--make AspectJ's annotations work - <Aop:aspectj-autoproxy></Aop:aspectj-autoproxy></Beans>
Spring's AOP-----helloword