pom.xml需要添加aop相关jar依赖(spring aop相关的依赖其实已经在core 和context中已经引入):
<!-- AspectJ 实现Spring AOP所需的三个依赖 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.11</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.11</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.1</version>
</dependency>
spring的配置文件需要添加的内容(spring.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"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<context:component-scan base-package="xyz.jangle.service">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>
<!-- aop相关的配置 -->
<!-- 配置注解扫面路径 -->
<context:component-scan base-package="xyz.jangle.aop" />
<!-- 开启注解 context:component-scan 已经涵盖了context:annotation-config的功能 -->
<!-- <context:annotation-config /> -->
<!-- 开启aspectj代理 -->
<aop:aspectj-autoproxy />
<!-- 此处省略数据库相关的配置 -->
</beans>
aop的切面编写(可能这个名词描述不太正确):
ExecuteIntercept.java:
package xyz.jangle.aop;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Service;
@Aspect
@Service
public class ExecuteIntercept {
@Pointcut("execution (* xyz.jangle.service.*.*(..))")
public void myPointcut() {}
@Before("myPointcut()")
public void beforeExecute() {
System.out.println("before execute to do something");
}
@After("myPointcut()")
public void afterExecute() {
System.out.println("after execute to do something");
}
@AfterReturning("myPointcut()")
public void afterReturnExecute() {
System.out.println("afterReturning execute to do something");
}
/*如果环绕通知不设置返回结果的话,那么service执行的方法也会被替代为不返回如何数据 测试的结果
可以这样理解: 环绕通知将切入点的方法进行了包装,或者说进行了代理。 故,如果不执行p.proceed()方法,那么被通知的方法将被阻塞,即不执行。
而如果不对p.proceed()方法的返回结果进行返回,则只执行切入点的方法,不返回任何数据。 这是需要注意的一点。
*/
@Around("myPointcut()")
public Object aroundExecute(ProceedingJoinPoint p) {
Object res =null;
try {
System.out.println("around befroe");
res = p.proceed();
System.out.println("around after");
System.out.println(res);
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return res;
}
}
UserServiceImpl.java:
package xyz.jangle.service.impl;
import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xyz.jangle.dao.UserMapper;
import xyz.jangle.model.User;
import xyz.jangle.service.UserService;
@Service
public class UserServiceImpl implements UserService {
private static Logger logger = Logger.getLogger(UserServiceImpl.class);
@Autowired
UserMapper userMapper;
@Override
public List<User> getAllUser() {
logger.info("UserServiceImpl Method.");
logger.info("这是一段中文");
System.out.println("这也是一段中文");
List<User> userList = userMapper.getAll();
/*for (User user : userList) {
System.out.println("id:" + user.getUserId());
System.out.println("age:" + user.getUserAge());
System.out.println("name:" + user.getUserName());
}*/
// User user = userMapper.findById(1);
// System.out.println("单用户信息查询,Name:" + user.getUserName());
return userList;
}
}
getAllUser方法(接口方法)将被pointcut匹配,那么,这个方法的执行结果会收到干预。
执行结果: