初学AOP

Posted 何甜甜在吗

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了初学AOP相关的知识,希望对你有一定的参考价值。

src\dayday\Count.java

package dayday;

import org.springframework.stereotype.Component;

/**
* Created by I am master on 2016/12/3.
*/
@Component
public 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.
*/
@Component
public 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
@Component
public 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>

以上是关于初学AOP的主要内容,如果未能解决你的问题,请参考以下文章

Aop 初学者

spring初学总结思路

初学AOP

Spring初学之annotation实现AOP前置通知和后置通知

Spring初学之annotation实现AOP前置通知后置通知返回通知异常通知。

Spring初学之xml实现AOP前置通知后置通知返回通知异常通知等