1). Add the jar package:
Spring-beans-4.1.6.release.jar
Commons-logging-1.1.3.jar
Spring-context-4.1.6.release.jar
Spring-core-4.1.6.release.jar
Spring-expression-4.1.6.release.jar
Spring-aop-4.1.6.release.jar
Spring-aspects-4.1.6.release.jar
Aspectjweaver-1.5.2a.jar
Aopalliance-1.0.jar
Need to download Aspectjweaver.jar package
2). Add the AOP namespace to the configuration file
3). Annotation-Based approach
1. Add the following configuration to the configuration file:
<AOP: aspectj-autoproxy></AOP:aspectj-autoproxy>
2. Abstract the code of crosscutting concerns into a class of facets
- The slice is first a bean in the IOC, which joins the @component annotation
- Facets also need to be added to @aspect annotations
3. Declare the various notifications in the class:
- Declaring a method
- Add @before annotations before the method
4. You can declare a parameter of type Joinpoint in the method again, and then you can access the link details, such as the method name and the parameter value
Code:
PackageCom.cn.spring.aop.impl;//interface classes for subtraction Public InterfaceArithmeticcalculator {intAddintIintj); intSubintIintj); intMulintIintj); intDivintIintj);}
PackageCom.cn.spring.aop.impl;Importorg.springframework.stereotype.Component;//Implementation Class@Component Public classArithmeticcalculatorimplImplementsArithmeticcalculator {@Override Public intAddintIintj) {intresult = i +J; returnresult; } @Override Public intSubintIintj) {intresult = i-J; returnresult; } @Override Public intMulintIintj) {intresult = i *J; returnresult; } @Override Public intDivintIintj) {intresult = I/J; returnresult; }}
PackageCom.cn.spring.aop.impl;ImportOrg.aspectj.lang.JoinPoint;ImportOrg.aspectj.lang.annotation.Aspect;ImportOrg.aspectj.lang.annotation.Before;Importorg.springframework.stereotype.Component;Importjava.util.Arrays;Importjava.util.List;//declare this class as a tangent: you first need to put the class in the IOC container, declared as a tangent plane@Aspect @component Public classLoggingaspect {//declares that the method is a pre-notification: Executes before the target method starts@Before ("Execution (public int arithmeticcalculator.* (int, int))") Public voidBeforemethod (Joinpoint joinpoint) {String methodName=joinpoint.getsignature (). GetName (); List<Object> args =arrays.aslist (Joinpoint.getargs ()); System.out.println ("The method" + MethodName + "begins with" +args); }}
PackageCom.cn.spring.aop.impl;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classMain { Public Static voidMain (string[] args) {//1. Create a spring IOC containerApplicationContext CTX =NewClasspathxmlapplicationcontext ("17-1.xml"); //2. Obtaining an instance of a bean from Huo in the IOC containerArithmeticcalculator arithmeticcalculator = Ctx.getbean (arithmeticcalculator.class); //3. Using Beans intresult = Arithmeticcalculator.add (3, 6); System.out.println ("Result:" +result); }}
<?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/beans http://www.springframework.org/schema/beans/spring-beans.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop.xsd "> <Context:component-scanBase-package= "Com.cn.spring.aop.impl"> </Context:component-scan> <!--make ASPJECTJ annotations work: Automatically generate proxy objects for matching classes - <Aop:aspectj-autoproxy></Aop:aspectj-autoproxy></Beans>
17Spring front-facing notification