Spring -- 基于注解的AOP开发
Posted CodeJiao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring -- 基于注解的AOP开发相关的知识,希望对你有一定的参考价值。
01:Spring – AOP简介
02:Spring – 基于XML的AOP开发
03:Spring – 基于注解的AOP开发
1. 快速入门
只要明白了基于XML的AOP开发,那么基于注解就很简单。
开发步骤:
准备工作:导入相关依赖
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">
<parent>
<artifactId>spring_study</artifactId>
<groupId>com.tian</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring_aop</artifactId>
<dependencies>
<!-- Spring依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<!-- aop织入依赖-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.4</version>
</dependency>
<!-- spring-test依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<!-- junit单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>
示例代码:
TargetInterface.java
package com.tian.anno;
public interface TargetInterface {
public void save();
}
Target.java
package com.tian.anno;
import org.springframework.stereotype.Component;
@Component("target")
public class Target implements TargetInterface {
public void save() {
System.out.println("save running.....");
}
}
MyAspect.java
package com.tian.anno;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Component("myAspect")
@Aspect //标注当前MyAspect是一个切面类
public class MyAspect {
//配置前置通知
@Before("execution(* com.tian.anno.*.*(..))")
public void before() {
System.out.println("前置增强..........");
}
@After("execution(* com.tian.anno.*.*(..))")
public void afterReturning() {
System.out.println("后置增强..........");
}
//Proceeding JoinPoint: 切点
// @Around("execution(* com.tian.anno.*.*(..))")
@Around("pointcut()")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("环绕前增强....");
Object proceed = pjp.proceed();//切点方法
System.out.println("环绕后增强....");
return proceed;
}
@AfterThrowing("MyAspect.pointcut()")
public void afterThrowing() {
System.out.println("异常抛出增强..........");
}
@After("MyAspect.pointcut()")
public void after() {
System.out.println("最终增强..........");
}
//定义切点表达式
@Pointcut("execution(* com.tian.anno.*.*(..))")
public void pointcut() {
}
}
配置文件:
applicationContext.xml
配置组件扫描和aop自动代理即可
<?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: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/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">
<!--组件扫描-->
<context:component-scan base-package="com.tian.anno"/>
<!--aop自动代理-->
<aop:aspectj-autoproxy/>
</beans>
测试代码:
AnnoTest.java
package test;
import com.tian.anno.TargetInterface;
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("classpath:applicationContext.xml")
public class AnnoTest {
@Autowired
private TargetInterface target;
@Test
public void test1() {
target.save();
}
}
运行结果:
2. 注解配置AOP详解
2.1 注解通知的类型
2.2 切点表达式的抽取
同xml配置 aop一样,我们可以将切点表达式抽取。抽取方式是在切面内定义方法,在该方法上使用@Pointcut注解定义切点表达式,然后在在增强注解中进行引用。具体如下:
3. 小结
以上是关于Spring -- 基于注解的AOP开发的主要内容,如果未能解决你的问题,请参考以下文章
SPRING03_AOP的概述动态代理cglib代理相关概念基于xml配置基于注解配置