1.项目流程
1)创建Maven项目,导入AOP相关坐标
2)创建目标接口和目标类,定义切入点
3)创建通知类
4)将目标类和通知类的创建权交给spring
5)在通知类中使用注解织入关系,升级为切面类
6)开启注解扫描和自动代理
7)创建测试类
1.创建Maven项目,导入AOP相关坐标
<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>aop_annotation</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--导入spring的context坐标,context依赖aop-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.5.RELEASE</version>
</dependency> <!-- aspectj的织入 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version> </dependency>
<!--spring整合junit-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency> </dependencies>
</project>
2.创建目标接口和目标类,定义切入点
- AccountService接口
package com.kk.service;
public interface AccountService {
public void transfer();
}
- AccountServiceImpl实现类
package com.kk.service;
import org.springframework.stereotype.Service;
public class AccountServiceImpl implements AccountService {
@Override
public void transfer() {
System.out.println("开始转账了!");
}
}
3.创建通知类
4.将目标类和通知类的创建权交给spring
- AccountServiceImpl实现类
package com.kk.service;
import org.springframework.stereotype.Service;
@Service
public class AccountServiceImpl implements AccountService {
@Override
public void transfer() {
System.out.println("开始转账了!");
}
}
- Advice类
package com.kk.advice;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Component
public class MyAdvice {
// 定义一个后置通知
public void afterReturning(){
System.out.println("转账过程结束");
}
}
5.在通知类中使用注解织入关系,升级为切面类
- Advice类
package com.kk.advice;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Component
@Aspect // 定义切面
public class MyAdvice {
// 定义一个后置通知
@AfterReturning("execution(* com.kk.service.AccountServiceImpl.*(..))")
public void afterReturning(){
System.out.println("转账过程结束");
}
}
6.开启注解扫描和自动代理
<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"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/context-beans.xsd">
<context:component-scan base-package="com.kk"></context:component-scan>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
7.创建测试类
import com.kk.service.AccountService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("applicationContext.xml")
public class AopAnnoTest {
// 因为调用了JDK动态代理类(因为实现了接口),所以这里使用AccountService
@Autowired
private AccountService accountService;
@Test
public void test(){
accountService.transfer();
}
}