Spring_AoP_annotation
2.导入必备jar包
pom.xml配置如下:
pom.xml
<?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>com.Taking</groupId>
<artifactId>spring_aop_annotation</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
</dependency>
</dependencies>
</project>
3.编写业务层的接口和实现类
3.1 新建Package,编写代码
3.1.1在src/main/java目录下,新建一个名为com.Taking.service的包(编写业务层的接口),再在包中新建一个名为StudentService的接口,代码如下:
StudentService.java
package com.Taking.service;
public interface StudentService {
void saveStudent();
void updateStudent(int i);
int deleteStudent();
}
3.1.2在src/main/java目录下,新建一个名为com.Taking.service.impl的包(编写业务层的实现类),再在包中新建一个名为StudentServiceImpl的类,代码如下:
StudentServiceImpl.java
package com.Taking.service.impl;
import com.Taking.service.StudentService;
import org.springframework.stereotype.Service;
@Service("studentService")
public class StudentServiceImpl implements StudentService {
public void saveStudent() {
System.out.println("执行了保存");
}
public void updateStudent(int i) {
System.out.println("执行了更新"+i);
}
public int deleteStudent() {
System.out.println("执行了删除");
return 0;
}
}
3.1.3在src/main/java目录下,新建一个名为com.Taking.utils的包(编写日志类),再在包中新建一个名为Logger的类,代码如下:
Logger.java
package com.Taking.utils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Component("logger")
@Aspect
public class Logger {
@Pointcut("execution( void com.Taking.service.impl.*.*(..))")
private void pt(){}
public void beforePrintLog(){
System.out.println("前置通知Logger类中的beforePrintLog方法开始记录日志了。。。");
}
public void afterReturningPrintLog(){
System.out.println("后置通知Logger类中的afterReturningPrintLog方法开始记录日志了。。。");
}
public void afterThrowingPrintLog(){
System.out.println("异常通知Logger类中的afterThrowingPrintLog方法开始记录日志了。。。");
}
public void afterPrintLog(){
System.out.println("最终通知Logger类中的afterPrintLog方法开始记录日志了。。。");
}
@Around("pt()")
public Object around(ProceedingJoinPoint pjp){
Object rtValue = null;
try{
Object[] args = pjp.getArgs();
System.out.println("around...前置");
rtValue = pjp.proceed();
System.out.println("around...后置");
} catch (Throwable t){
System.out.println("around...异常");
throw new RuntimeException(t);
} finally {
System.out.println("around...最终");
}
return rtValue;
}
}
4.编写配置类
4.1 新建Package,编写代码
4.1.1在src/main/java目录下,新建一个名为com.Taking.config的包(编写配置类),再在包中新建一个名为SpringConfiguration的类,代码如下:
SpringConfiguration.java
package com.Taking.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@ComponentScan("com.Taking")
@EnableAspectJAutoProxy
public class SpringConfiguration {
}
5.编写测试类
5.1 新建Package,编写测试代码
5.1.1 在src/test/java目录下,新建一个名为com.Taking.test的包(编写测试类),再在包中新建一个名为AOPTest的类,代码如下:
AOPTest.java
package com.Taking.test;
import com.Taking.config.SpringConfiguration;
import com.Taking.service.StudentService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class AOPTest {
public static void main(String[] args) {
ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);
StudentService studentService = ac.getBean("studentService",StudentService.class);
studentService.saveStudent();
}
}
6.测试结果
Spring_bug: 输出结果有误(和Spring_Aop_xml一样),此处就不截图了
只使用环绕通知的方法(将其他的注解注释掉),在方法里进行控制,结果如下图所示:
执行顺序
通知类型:前置通知,后置通知,异常通知,最终通知,环绕通知
执行顺序:前置通知
->环绕通知(前置->后置/异常->最终)
public Object around(ProceedingJoinPoint pjp){
Object rtValue = null;
try{
Object[] args = pjp.getArgs();
System.out.println("around...前置");
rtValue = pjp.proceed();
System.out.println("around...后置");
} catch (Throwable t){
System.out.println("around...异常");
throw new RuntimeException(t);
} finally {
System.out.println("around...最终");
}
return rtValue;
}
->后置通知/异常通知
->最终通知
本文是基于注解方式实现Spring中AOP的演示