[Spring] AOP, Aspect实例解析

时间:2022-10-17 05:32:53

最近要用到切面来统一处理日志记录,写了个小实例练了练手:

具体实现类:

public interface PersonServer {
public void save(String name);
public void update(String name, Integer id);
public String getPersonName(Integer id);
}
import org.springframework.stereotype.Component;

@Component("personServerBean")
public class PersonServerBean implements PersonServer {
public void save(String name) {
System.out.println("save方法");
}
public void update(String name, Integer id) {
System.out.println("update方法");
}
public String getPersonName(Integer id) {
System.out.println("getPersonName方法");
return "myName";
}
}

切面类:

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component; @Aspect
@Component
public class MyInterceptor {
@Pointcut("execution(* com.alibaba.aop.PersonServerBean.*(..))")
private void anyMethod(){} @Before("anyMethod()")
public void doAccessCheck() {
System.out.println("前置通知");
}
@After("anyMethod()")
public void after() {
System.out.println("最终结果");
}
@AfterReturning("anyMethod()")
public void doAfter() {
System.out.println("后置通知");
}
@AfterThrowing("anyMethod()")
public void doAfterThrow() {
System.out.println("例外通知");
}
@Around("anyMethod()")
public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("进入环绕通知");
Object object = pjp.proceed();
System.out.println("退出方法");
return object;
}
// 顺序: before->method->after->afterReturning
}

XML文件配置:

<?xml version="1.0" encoding="GBK"?>
<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:annotation-config />这个配置可以去掉,在aspject-autoproxy里面包含了这个配置--> <aop:aspectj-autoproxy /> <context:component-scan base-package="com...." />
<!--<import resource="classpath:tutorial-beans.xml" /> 写到其他文件里面去,引入-->
</beans>

引用jar包pom文件配置:

<aspect-version>1.8.0</aspect-version>
<spring-version>3.2.7.RELEASE</spring-version>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspect-version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspect-version}</version>
</dependency>

执行测试类:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component; @Component("springAOPTest")
public class SpringAOPTest {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
PersonServer bean = (PersonServerBean) ctx.getBean("personServerBean");
bean.save("test");
}
}

执行结果:

进入环绕通知
前置通知
save方法
退出方法
最终结果
后置通知

总结-执行顺序:around->before->around->after->afterReturning

参考连接:http://blog.csdn.net/wangpeng047/article/details/8556800