首先。需要导入一个依赖包
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjmatcher</artifactId>
<version>1.9.6</version>
</dependency>
</dependencies>
方式一:使用Spring的接口
- 编写模拟业务层代码
public interface UserService {
public void add();
public void delete();
public void update();
public void select();
}
public class UserServiceImpl implements UserService {
@Override
public void add() {
System.out.println("增加了一个用户");
}
@Override
public void delete() {
System.out.println("删除了一个用户");
}
@Override
public void update() {
System.out.println("修改了一个用户");
}
@Override
public void select() {
System.out.println("查询了一个用户");
}
}
- 编写模拟日志的代码:
public class Log implements MethodBeforeAdvice {
//method:要执行的目标对象的方法
//object:参数
@Override
public void before(Method method, Object[] objects, Object taeget) throws Throwable {
System.out.println(taeget.getClass().getName()+"的"+method.getName()+"被执行了");
}
}
public class AfterLog implements AfterReturningAdvice {
@Override
//returnValue:返回值
public void afterReturning(Object returnValue, Method method, Object[] objects, Object target) throws Throwable {
System.out.println("执行了"+method.getName()+"的方法,返回结果为"+returnValue);
}
}
- 编写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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--注册bean-->
<bean id="userService" class="com.yf.service.UserServiceImpl"/>
<bean id="log" class="com.yf.log.Log"/>
<bean id="afterLog" class="com.yf.log.AfterLog"/>
<!--方式一:使用原生的Spring api-->
<!--配置aop:需要导入aop的约束-->
<aop:config>
<!--切入点 expression 表达式(要执行的位置!)-->
<aop:pointcut id="pointcut" expression="execution(* com.yf.service.UserServiceImpl.*(..))"/>
<!--执行环绕-->
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>
</beans>
- 编写测试类
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = context.getBean("userService", UserService.class);
userService.add();
userService.delete();
}
}
方式二:使用自定义来实现AOP
public class DiyPointCut {
public void before(){
System.out.println("========方法执行前==========");
}
public void after(){
System.out.println("========方法执行后===========");
}
}
<bean id="diy" class="com.yf.diy.DiyPointCut"/>
<aop:config>
<!--自定义切面-->
<aop:aspect ref="diy">
<!--切入点-->
<aop:pointcut id="point" expression="execution(* com.yf.service.UserServiceImpl.*(..))"/>
<!--通知-->
<aop:before method="before" pointcut-ref="point"/>
<aop:after method="after" pointcut-ref="point"/>
</aop:aspect>
</aop:config>
方式三:使用注解实现
@Aspect //标注这个类是个切面
public class AnnocationPointCut {
@Before("execution(* com.yf.service.UserServiceImpl.*(..))")
public void before(){
System.out.println("=========方法执行前==========");
}
@After("execution(* com.yf.service.UserServiceImpl.*(..))")
public void after(){
System.out.println("====方法执行后");
}
//在环绕增强中,我们可以给定一个参数,代表我们要处理切入的点
@Around("execution(* com.yf.service.UserServiceImpl.*(..))")
public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("====环绕前====");
// System.out.println(proceedingJoinPoint.getSignature());
//执行方法
Object proceed = proceedingJoinPoint.proceed();
//环绕后
System.out.println("=====环绕后=====");
}
}
注册bean
<bean id="aaa" class="com.yf.AnnotationPointCut.AnnocationPointCut"/>
<aop:aspectj-autoproxy/>