创建一个名为spring02 的maven web项目
在src/main下创建java包和resources包
在pom.xml中导入spring和aspectjweaver依赖
<!-- spring-webmvc 可以导入spring全部核心jar包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
创建一个AnnotationPointCut类
package com.lijunbo.diy;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
//方式三:使用注解方式实现AOP
@Aspect //标注这个类是一个切面
public class AnnotationPointCut {
@Before("execution(* com.lijunbo.service.impl.UserServiceImpl.*(..))")
public void before() {
System.out.println("方法执行前");
}
@After("execution(* com.lijunbo.service.impl.UserServiceImpl.*(..))")
public void after() {
System.out.println("方法执行后");
}
//在环绕增强中,可以给定一个参数,代表要获取处理切入的点
@Around("execution(* com.lijunbo.service.impl.UserServiceImpl.*(..))")
public void around(ProceedingJoinPoint jp) throws Throwable {
System.out.println("环绕前");
Signature signature = jp.getSignature();//获得签名
System.out.println("执行了" + signature + "方法");
//执行方法
Object proceed = jp.proceed();
System.out.println("返回的结果为:" + proceed);
System.out.println("环绕后");
}
}
创建service层模拟一个添加方法
package com.lijunbo.service;
public interface UserService {
public void add();
}
package com.lijunbo.service.impl;
import com.lijunbo.service.UserService;
public class UserServiceImpl implements UserService {
@Override
public void add(){
System.out.println("增加了一个用户");
}
}
创建applicationContext.xml配置service的bean
<?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
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
">
<bean id="userService" class="com.lijunbo.service.impl.UserServiceImpl"/>
</beans>
在applicationContext.xml中加入aop的注解支持并注册AnnotationPointCut的bean
<bean id="annotationPointCut" class="com.lijunbo.diy.AnnotationPointCut"/>
<aop:aspectj-autoproxy/>
创建一个AOP类
package com.lijunbo;
import com.lijunbo.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestAOP {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = context.getBean("userService", UserService.class);
userService.add();
}
}
测试成功