Spring AOP - Aspect Oriented Programming 面向切面编程。
AOP的做法是将通用、与业务无关的功能抽象封装为切面类。
切面可配置在目标方法的执行前、后运行,真正做到即插即用。
最终目的:在不修改源码的情况下对程序行为进行扩展。
打开IDEA创建新的maven工程,在pom.xml中引入spring-context和aspectjweaver依赖
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.ql.springgroupId>
<artifactId>aopartifactId>
<version>1.0-SNAPSHOTversion>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
properties>
<repositories>
<repository>
<id>aliyunid>
<name>aliyunname>
<url>https://maven.aliyun.com/repository/publicurl>
repository>
repositories>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>5.2.6.RELEASEversion>
dependency>
<dependency>
<groupId>org.aspectjgroupId>
<artifactId>aspectjweaverartifactId>
<version>1.9.5version>
dependency>
dependencies>
project>
在com.ql.spring.aop.dao包下创建UserDao和EmployeeDao类
package com.ql.spring.aop.dao;
public class UserDao {
public void insert(){
System.out.println("新增用户数据");
}
}
package com.ql.spring.aop.dao;
public class EmployeeDao {
public void insert(){
System.out.println("新增员工数据");
}
}
在com.ql.spring.aop.service包下创建UserService和EmployeeService类
package com.ql.spring.aop.service;
import com.ql.spring.aop.dao.UserDao;
public class UserService {
private UserDao userDao;
public void createUser(){
System.out.println("执行创建用户业务逻辑");
userDao.insert();
}
public String generateRandomPassword(String type, Integer length){
System.out.println("按"+type+"方式生成"+length+"位随机密码");
return "HTYughjd23g";
}
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
}
package com.ql.spring.aop.service;
import com.ql.spring.aop.dao.EmployeeDao;
public class EmployeeService {
private EmployeeDao employeeDao;
public void entry(){
System.out.println("执行员工入职业务逻辑");
employeeDao.insert();
}
public EmployeeDao getEmployeeDao() {
return employeeDao;
}
public void setEmployeeDao(EmployeeDao employeeDao) {
this.employeeDao = employeeDao;
}
}
在com.ql.spring.aop.aspect包下创建切面类MethodAspect
package com.ql.spring.aop.aspect;
import org.aspectj.lang.JoinPoint;
import java.text.SimpleDateFormat;
import java.util.Date;
//切面类
public class MethodAspect {
//切面方法,用于扩展额外功能
//JoinPoint 连接点,通过连接点可以获取目标类/方法的信息
public void printExecutionTime(JoinPoint joinPoint){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
String now = sdf.format(new Date());
String className = joinPoint.getTarget().getClass().getName();//获取目标类的名称
String methodName = joinPoint.getSignature().getName();//获取目标方法名称
System.out.println("-------->"+now+":"+className+"."+methodName);
}
}
在resources目录下创建配置文件applicationContext.xml
<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 id="userDao" class="com.ql.spring.aop.dao.UserDao"/>
<bean id="employeeDao" class="com.ql.spring.aop.dao.EmployeeDao"/>
<bean id="userService" class="com.ql.spring.aop.service.UserService">
<property name="userDao" ref="userDao"/>
bean>
<bean id="employeeService" class="com.ql.spring.aop.service.EmployeeService">
<property name="employeeDao" ref="employeeDao"/>
bean>
<bean id="methodAspect" class="com.ql.spring.aop.aspect.MethodAspect"/>
<aop:config>
<aop:pointcut id="pointcut" expression="execution(public * com.ql..*.*(..))"/>
<aop:aspect ref="methodAspect">
<aop:before method="printExecutionTime" pointcut-ref="pointcut"/>
aop:aspect>
aop:config>
beans>
最后在com.ql.spring.aop包下创建入口类SpringApplication并运行
package com.ql.spring.aop;
import com.ql.spring.aop.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringApplication {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
UserService userService = context.getBean("userService", UserService.class);
userService.createUser();
}
}
/*
-------->2022-07-03 14:02:54 192:com.ql.spring.aop.service.UserService.createUser
执行创建用户业务逻辑
-------->2022-07-03 14:02:54 208:com.ql.spring.aop.dao.UserDao.insert
新增用户数据
*/
Eclipse AspectJ,一种基于Java平台的面向切面编程的语言。
Spring AOP使用AspectJWeaver实现类与方法匹配。
SpringAOP利用代理模式实现对象运行时功能扩展。
依赖AspectJ
实现切面类/方法
配置Aspect Bean
定义PointCut
配置Advice
package com.ql.spring.aop.aspect;
import org.aspectj.lang.JoinPoint;
import java.text.SimpleDateFormat;
import java.util.Date;
//切面类
public class MethodAspect {
//切面方法,用于扩展额外功能
//JoinPoint 连接点,通过连接点可以获取目标类/方法的信息
public void printExecutionTime(JoinPoint joinPoint){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
String now = sdf.format(new Date());
String className = joinPoint.getTarget().getClass().getName();//获取目标类的名称
String methodName = joinPoint.getSignature().getName();//获取目标方法名称
System.out.println("-------->"+now+":"+className+"."+methodName);
Object[] args = joinPoint.getArgs();
System.out.println("-------->参数个数:"+args.length);
for (Object arg: args){
System.out.println("-------->参数:"+arg);
}
}
}
然后再修改入口类SpringApplication并运行
package com.ql.spring.aop;
import com.ql.spring.aop.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringApplication {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
UserService userService = context.getBean("userService", UserService.class);
userService.createUser();
userService.generateRandomPassword("MD5",16);
}
}
/*
-------->2022-07-03 16:26:43 265:com.ql.spring.aop.service.UserService.createUser
-------->参数个数:0
执行创建用户业务逻辑
-------->2022-07-03 16:26:43 278:com.ql.spring.aop.dao.UserDao.insert
-------->参数个数:0
新增用户数据
-------->2022-07-03 16:26:43 284:com.ql.spring.aop.service.UserService.generateRandomPassword
-------->参数个数:2
-------->参数:MD5
-------->参数:16
按MD5方式生成16位随机密码
*/
//只对Service类生效
execution(* com.ql..*Service.*(..))
//只对返回值为String类型方法有效
execution(String com.ql..*Service.*(..))
//只对两个参数的方法有效
execution(* com.ql..*Service.*(*,*))