Implements the subtraction of two integers and prints the log before and after each calculation.
Arithmeticcalculator.java:
package Spring.aop.impl; public interface Arithmeticcalculator { int Add (int i,int J); int Sub (int i,int J); int mul (int i,int J); int div (int i,int J); }
Arithmeticcalculatorimpl.java:
PackageSpring.aop.impl;Importorg.springframework.stereotype.Component; @Component ("Arithmeticcalculator") Public classArithmeticcalculatorimplImplementsArithmeticcalculator { Public intAddintIintj) {intresult = i+J; returnresult; } Public intSubintIintj) {intresult = IJ; returnresult; } Public intMulintIintj) {intresult = i*J; returnresult; } Public intDivintIintj) {intresult = i/J; returnresult; }}
Loggingaspect.java:
PackageSpring.aop.impl;Importjava.util.Arrays;Importjava.util.List;ImportOrg.aspectj.lang.JoinPoint;ImportOrg.aspectj.lang.annotation.After;ImportOrg.aspectj.lang.annotation.Aspect;ImportOrg.aspectj.lang.annotation.Before;Importorg.springframework.stereotype.Component;//declare this class as a tangent: 1. Put the class into the IOC container, 2. Declare a tangent@Aspect @component Public classLoggingaspect {//declares that the method is a pre-notification: Executes before the target method@Before ("Execution (public int spring.aop.impl.*.* (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" +args); } //Post notification: A notification that executes after the target method executes (regardless of whether an exception occurred). //cannot access the execution result of the target method in the post notification@After ("Execution (public int spring.aop.impl.*.* (int,int))") Public voidAftermethod (Joinpoint joinpoint) {String methodName=joinpoint.getsignature (). GetName (); System.out.println ("The method" +methodname+ "ends"); } }
Applicationcontext.xml:
<?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-4.3.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/ Spring-aop-4.3.xsd "> <!--Configure automatic scanning of packages - <Context:component-scanBase-package= "Spring.aop.impl" /> <!--make ASPECTJ annotations work, automatically generate proxy objects for matching classes - <Aop:aspectj-autoproxy></Aop:aspectj-autoproxy></Beans>
Test:
Packagespring.aop.impl.test;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;ImportSpring.aop.impl.ArithmeticCalculator; Public classMain { Public Static voidMain (string[] args) {//1. Create a spring IOC containerApplicationContext ctx=NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); //2. Get an instance of the bean from the containerArithmeticcalculator arithmeticcalculator= (arithmeticcalculator) Ctx.getbean ("Arithmeticcalculator"); //3. Using Beans intresult = Arithmeticcalculator.add (5, 8); System.out.println ("Result:" +result); Result= Arithmeticcalculator.div (5, 8); System.out.println ("Result:" +result); }}
Output:
The method add begins[5, 8]the method Add Endsresult:themethod Div begins[5, 8]the method Div End Sresult:0
Spring Beginner's annotation implement AOP pre-notification and post-notification