src\dayday\Count.java
package dayday; import org.springframework.stereotype.Component; /** * Created by I am master on 2016/12/3. */@Componentpublic interface Count { int add(int i, int j); int mul(int i, int j); int div(int i, int j); int sub(int i, int j);}
src\dayday\CountImpl.java
package dayday; import dayday.Count;import org.springframework.stereotype.Component; /** * Created by I am master on 2016/12/3. */@Componentpublic class CountImpl implements Count { public int add(int i,int j){ int result=i+j; return result; } public int sub(int i,int j){ int result=i-j; return result; } public int mul(int i,int j){ int result=i*j; return result; } public int div(int i,int j){ int result=i/j; return result; }}
src\dayday\LogginAspect.java
package dayday; import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.*; import org.springframework.stereotype.Component; import java.util.Arrays;import java.util.List; /** * Created by I am master on 2016/12/3. */@Aspect@Componentpublic class LogginAspect { @Before("execution(public int dayday.Count.add(int,int))") public void beforeMethod(JoinPoint joinpoint){ String methodName=joinpoint.getSignature().getName(); List<Object>args= Arrays.asList(joinpoint.getArgs()); System.out.println("method is "+methodName+" begins with "+args); } @After("execution(public int dayday.Count.add(int,int))") public void afterMethod(){ System.out.println("After..."); } @AfterReturning(value="execution(public int dayday.Count.add(int,int))",returning = "result") public void afterReturning(JoinPoint joinpoint,Object result){ String methodName=joinpoint.getSignature().getName(); System.out.println("method is "+methodName+" result is "+result); } @AfterThrowing(value="execution(public int dayday.Count.div(int,int))",throwing ="exception") public void afterThrowing(JoinPoint joinpoint,Exception exception){ String methodName=joinpoint.getSignature().getName(); System.out.println("method is "+methodName+" occurs "+exception); } @Around("execution(public int dayday.Count.add(int,int))") public Object aroundMethod(ProceedingJoinPoint pjd){ Object result = null; String methodName = pjd.getSignature().getName(); try { //前置通知 System.out.println("The method " + methodName + " begins with " + Arrays.asList(pjd.getArgs())); //执行目标方法 result = pjd.proceed(); //返回通知 System.out.println("The method " + methodName + " ends with " + result); } catch (Throwable e) { //异常通知 System.out.println("The method " + methodName + " occurs exception:" + e); throw new RuntimeException(e); } //后置通知 System.out.println("The method " + methodName + " ends"); return result; }}
src\dayday\Main.java
package dayday; import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Created by I am master on 2016/12/3. */public class Main { public static void main(String[] args){ ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml"); Count c = ctx.getBean(Count.class); int result=c.add(8,3); }}
src\beans.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="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-scan base-package="dayday"/> <!--使AspectJ注释起作用,自动为匹配的类生成代理对象--> <aop:aspectj-autoproxy/></beans>
时间: 2024-10-27 11:49:36