引入相关包:
<properties>
<spring.version>3.0.5.RELEASE</spring.version>
<aspectj.version>1.6.11</aspectj.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.1</version>
</dependency>
</dependencies>
在Spring配置文件开启注解、AspectJ支持、扫描基础包:
<?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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- 开启注解 -->
<context:annotation-config/>
<!-- AspectJ支持 -->
<aop:aspectj-autoproxy />
<!-- 扫描基础包 -->
<context:component-scan base-package="com.nicchagil.springaop" />
</beans>
写两个测试的Service:
package com.nicchagil.springaop;
import org.springframework.stereotype.Service;
@Service
public class UserService {
public void query(Integer id) {
System.out.println("UserService.query()...");
}
}
package com.nicchagil.springaop;
import org.springframework.stereotype.Service;
@Service
public class FunctionService {
public void query() {
System.out.println("FunctionService.query()...");
}
}
切面的信息:
package com.nicchagil.springaop;
import java.util.Arrays;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class MyAOP {
@Pointcut("execution(* com.nicchagil.springaop.*Service.query(..))")
public void myPointcut() {
}
@Before("myPointcut()")
public void myBefore(JoinPoint joinPoint) {
Object[] args = joinPoint.getArgs(); // 入参
System.out.println("Before. Args : " + Arrays.toString(args));
}
@AfterReturning("myPointcut()")
public void myAfterReturning() {
System.out.println("AfterReturning.");
}
@AfterThrowing("myPointcut()")
public void myAfterThrowing() {
System.out.println("AfterThrowing.");
}
}
入口类:
package com.nicchagil.springaop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HowToUse {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("spring.xml");
UserService us = context.getBean("userService", UserService.class);
us.query(100);
FunctionService fs = context.getBean("functionService", FunctionService.class);
fs.query();
}
}
日志:
Before. Args : [100]
UserService.query()...
AfterReturning.
Before. Args : []
FunctionService.query()...
AfterReturning.