抱歉,您的浏览器无法访问本站

本页面需要浏览器支持(启用)JavaScript


了解详情 >

快速入门

  1. 导入AOP相关坐标
1
2
3
4
5
6
7
8
9
10
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
</dependency>
  1. 创建目标接口和目标类(内部有切点)
1
2
3
4
5
6
7
8
9
10
11
package cn.imqinhao.aop;

/**
* @author qinhao
* @version 1.0
*/
public interface TargetInterface {

public void save();

}
1
2
3
4
5
6
7
8
9
10
11
12
package cn.imqinhao.aop;

/**
* @author qinhao
* @version 1.0
*/
public class Target implements TargetInterface{
@Override
public void save() {
System.out.println("save running...");
}
}
  1. 创建切面类(内部有增强方法)
1
2
3
4
5
6
7
8
9
10
11
12
13
package cn.imqinhao.aop;

/**
* @author qinhao
* @version 1.0
*/
public class MyAspect {

public void before() {
System.out.println("前置增强...");
}

}
  1. 将目标类和切面类的对象创建权交给Spring
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?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: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
">

<!--目标对象-->
<bean id="target" class="cn.imqinhao.aop.Target" />

<!--切面对象-->
<bean id="myAspect" class="cn.imqinhao.aop.MyAspect" />

</beans>
  1. 在 applicationContext.xml 中配置织入关系
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
<?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: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
">

<!--目标对象-->
<bean id="target" class="cn.imqinhao.aop.Target" />

<!--切面对象-->
<bean id="myAspect" class="cn.imqinhao.aop.MyAspect" />

<!--配置织入:告诉Spring框架 哪些方法(切点)需要哪些增强(前置、后置...)-->
<aop:config>
<!--声明切面-->
<aop:aspect ref="myAspect">
<!--切面:切点 + 通知-->
<aop:before method="before" pointcut="execution(public void cn.imqinhao.aop.Target.save())" />
</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
package cn.imqinhao.test;

import cn.imqinhao.aop.Target;
import cn.imqinhao.aop.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;

/**
* @author qinhao
* @version 1.0
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class AopTest {

@Autowired
private TargetInterface target;

@Test
public void test1() {
target.save();
}

}

运行结果:

前置增强…
save running…

AOP切点表达式的写法

表达式语法:

1
execution([修饰符] 返回值类型 包名.类名.方法名(参数))
  • 访问修饰符可以忽略
  • 返回值类型、包名、类名、方法名可以使用星号 * 代表任意
  • 包名与类名之间一个点 . 代表当前包下的类,两个点 .. 表示当前包及其子包下的类
  • 参数列表可以使用两个点 .. 代表任意个数,任意类型的参数列表

所以,上面的applicationContext.xml文件的第21行我们可以修改一下:

1
<aop:before method="before" pointcut="execution(* cn.imqinhao.aop.*.*(..))" />

通知的类型

通知的配置语法:

1
<aop:通知类型 method="切面类中方法名" pointcut="切点表达式"></aop:通知类型>
名称 标签 说明
前置通知 <aop:before> 用于配置前置通知。指定增强的方法在切入点方法之前执行。
后置通知 <aop:after-returning> 用于配置后置通知。指定增强的方法在切入点方法之后执行。
环绕通知 <aop:around> 用于配置环绕通知。指定增强的方法在切入点之前和之后都执行。
异常抛出通知 <aop:throwing> 用于配置异常抛出通知。指定增强的方法在出现异常时执行。
最终通知 <aop:after> 用于配置最终通知。无论增强方法执行是否有异常都会执行。

切点表达式的抽取

当多个增强的切点表达式相同时,可以将切点表达式进行抽取,在增强中使用pointcut-ref属性代替pointcut属性来引用抽取后的切点表达式。

接下来我们对 applicationContext.xml 进行优化:

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
<?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: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
">

<!--目标对象-->
<bean id="target" class="cn.imqinhao.aop.Target" />

<!--切面对象-->
<bean id="myAspect" class="cn.imqinhao.aop.MyAspect" />

<!--配置织入:告诉Spring框架 哪些方法(切点)需要哪些增强(前置、后置...)-->
<aop:config>
<!--声明切面-->
<aop:aspect ref="myAspect">
<!--抽取切点表达式-->
<aop:pointcut id="myPointCut" expression="execution(* cn.imqinhao.aop.*.*(..))"/>
<aop:around method="around" pointcut-ref="myPointCut" />
<aop:after method="after" pointcut-ref="myPointCut" />
</aop:aspect>
</aop:config>
</beans>

运行结果:

环绕前增强…
save running…
环绕后增强…
最终增强…

评论




2019-2022 覃浩的博客 鄂ICP备2021017381号-1 正在载入...

PoweredHexo
HostedGitHub & Coding
DNSDNSPod
CDN腾讯云CDN
PictureBed腾讯云CDN
ThemeVolantis