# 1 初始AOP
# 1-1介绍AOP
AOP面向切面编程
- Spring AOP与相关概念名词
- Spring AOP开发与配置流程
- Spring 五种通知类型与应用场景
# Spring AOP
- Spring AOP - Aspect Oriented Programming 面向切面编程
- AOP的做法是将通用、与业务无关的功能抽象封装为切面类
- 切面可配置在目标方法的执行前、后运行,真正做到即插即用
Spring AOP切面编程,最终目的是:在不修改源码的情况下对程序行为进行扩展
# 1-2 初始AOP编程一
pom.xml
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.5</version>
</dependency>
<!--aspectjweaver是Spring AOP的底层依赖,也就是说Spring AOP只有引入aspectjweaver之后,才能正常的运行-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.6</version>
</dependency>
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
package com.torey.spring.aop.aspect;
import org.aspectj.lang.JoinPoint;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.SimpleFormatter;
/**
* @Author http://torey611.gitee.io/li-tao-feng/
* @Email torey6061@qq.com
* @Date 2021/5/1 16:21
* @描述: 切面类
*/
public class MethodAspect {
/**
* 切面方法,用于扩展额外的功能
* @param joinPoint 连接点,通过连接点可以获取目标类/方法的信息
*/
public void printExecutionTime(JoinPoint joinPoint){
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
String nowDateStr = sdf.format(new Date());
//获取目标类的名称
String className= joinPoint.getTarget().getClass().getName();
//获取目标方法名称
String methoName = joinPoint.getSignature().getName();
Object[] args = joinPoint.getArgs();
System.out.println("---->"+nowDateStr+":"+className+"."+methoName);
if (args.length>0) {
System.out.println("====存在参数,参入如下====");
for (Object arg : args) {
System.out.println("参数类型为:"+ arg.getClass()+"参数值为:"+ arg);
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.torey.spring.aop.dao;
/**
* @Author http://torey611.gitee.io/li-tao-feng/
* @Email torey6061@qq.com
* @Date 2021/5/1 15:23
* @描述:
*/
public class EmployeeDao {
public void insert(){
System.out.println(this.getClass().getName() + ".insert();新增数据");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
package com.torey.spring.aop.dao;
/**
* @Author http://torey611.gitee.io/li-tao-feng/
* @Email torey6061@qq.com
* @Date 2021/5/1 15:22
* @描述:
*/
public class UserDao {
public void insert(){
System.out.println(this.getClass().getName() + ".insert();新增数据");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.torey.spring.aop.service;
import com.torey.spring.aop.dao.EmployeeDao;
/**
* @Author http://torey611.gitee.io/li-tao-feng/
* @Email torey6061@qq.com
* @Date 2021/5/1 15:24
* @描述:
*/
public class EmployeeService {
private EmployeeDao employeeDao;
public void entry(){
System.out.println(this.getClass().getName() + ".entry();执行员工入职业务逻辑");
employeeDao.insert();;
}
public EmployeeDao getEmployeeDao() {
return employeeDao;
}
public void setEmployeeDao(EmployeeDao employeeDao) {
this.employeeDao = employeeDao;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.torey.spring.aop.service;
import com.torey.spring.aop.dao.UserDao;
/**
* @Author http://torey611.gitee.io/li-tao-feng/
* @Email torey6061@qq.com
* @Date 2021/5/1 15:23
* @描述:
*/
public class UserService {
private UserDao userDao;
public void createUser(){
System.out.println(this.getClass().getName() + ".createUser();执行创建用户业务逻辑");
userDao.insert();
}
public String generateRandomPassWord(String type,Integer length){
System.out.println("按" + type + "方式生成" + length + "位随机密码");
return "ddsseeee";
}
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.torey.spring.aop;
import com.torey.spring.aop.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @Author http://torey611.gitee.io/li-tao-feng/
* @Email torey6061@qq.com
* @Date 2021/5/1 15:55
* @描述:
*/
public class SpringApplication {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
UserService bean = context.getBean("userService", UserService.class);
bean.createUser();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
applicationContext.xml文件如下:
从这里下载复制schema (opens new window)
<?xml version="1.0" encoding="UTF-8"?>
<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="http://www.springframework.org/schema/beans"
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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="userDao" class="com.torey.spring.aop.dao.UserDao"></bean>
<bean id="employeeDao" class="com.torey.spring.aop.dao.EmployeeDao"></bean>
<bean id="userService" class="com.torey.spring.aop.service.UserService">
<property name="userDao" ref="userDao"></property>
</bean>
<bean id="employeeService" class="com.torey.spring.aop.service.EmployeeService">
<property name="employeeDao" ref="employeeDao"></property>
</bean>
<!--AOP配置-->
<bean id="methodAspect" class="com.torey.spring.aop.aspect.MethodAspect"></bean>
<aop:config>
<!--pointcut 切点,使用execution表达式描述切面的作用范围-->
<!--execution(public * com.torey..*.*(..)) 说明切面作用在com.torey包下的所有类的所有方法上-->
<aop:pointcut id="pointcut" expression="execution(public * com.torey..*.*(..))"></aop:pointcut>
<!--定义切面类-->
<aop:aspect ref="methodAspect">
<!--before通知(Advice),代表在目标方法运行前先执行MethodAspect.printExecutionTime()-->
<!--<aop:after method="printExecutionTime" pointcut-ref="pointcut"></aop:after>-->
<aop:before method="printExecutionTime" pointcut-ref="pointcut"></aop:before>
</aop:aspect>
</aop:config>
</beans>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
运行后,输出如下:
---->2021-05-01 20:49:03 480:com.torey.spring.aop.service.UserService.createUser
com.torey.spring.aop.service.UserService.createUser();执行创建用户业务逻辑
---->2021-05-01 20:49:03 505:com.torey.spring.aop.dao.UserDao.insert
com.torey.spring.aop.dao.UserDao.insert();新增数据
1
2
3
4
2
3
4
# 导航,上一页,下一页
# 支持我-微信扫一扫-加入微信公众号
# 赞赏作者
# 种一棵树,最好的时间是十年前,其次是现在
立志用功,如种树然。方其根芽,犹未有干;及其有干,尚未有枝;枝而后叶,叶而后花、实。初种根时,只管栽培灌溉,勿作枝想,勿作实想。悬想何益?何不忘栽培之功,怕没有枝叶花实?