1
2 1------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------
2
RegexpMethodPointcutAdvisor:正则方法切入点顾问
核心:
<property name="pattern" value=".\*do.\*"></property> 表示方法全名(包名,接口名,方法名)
运算符 名称 意义
. 点号 表示任意单个字符
+ 加号 表示前一个字符出现一次或者多次
* 星号 表示前一个字符出现0次或者多次
具体使用案例:
一个实现俩个方法的类:SomeServiceImpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 1package cn.dawn.day16advisor02;
2
3
4/**
5 * Created by Dawn on 2018/3/8.
6 */
7public class SomeServiceImpl {
8 public void doSome() {
9 System.out.println("do something");
10 }
11 public void doAny() {
12 System.out.println("do Any");
13 }
14}
15
一个实现任意增强的接口的方法:此处是实现后置增强的,我起名LoggerAfter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 1package cn.dawn.day16advisor02;
2
3import org.springframework.aop.AfterReturningAdvice;
4
5import java.lang.reflect.Method;
6
7/**
8 * Created by Dawn on 2018/3/5.
9 */
10/*后置增强*/
11public class LoggerAfter implements AfterReturningAdvice {
12 public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
13 System.out.println("===============after==================");
14 }
15}
16
大配置文件:
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 1<?xml version="1.0" encoding="UTF-8"?>
2<beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:aop="http://www.springframework.org/schema/aop"
5 xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
6 xsi:schemaLocation="http://www.springframework.org/schema/beans
7 http://www.springframework.org/schema/beans/spring-beans.xsd
8 http://www.springframework.org/schema/aop
9 http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
10 <!--要增强的对象-->
11 <bean id="service" class="cn.dawn.day16advisor02.SomeServiceImpl"></bean>
12 <!--增强的内容-->
13 <bean id="myadvice" class="cn.dawn.day16advisor02.LoggerAfter"></bean>
14 <!--顾问-->
15 <bean id="myadvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
16 <property name="advice" ref="myadvice"></property>
17 <property name="pattern" value=".*do.*"></property>
18 <!--上面那个name还有一个可以是patterns,value可以写多个正则。用逗号分割,匹配多个正则,方法名只要合适其中一个正则就增强-->
19 </bean>
20 <!--代理工厂bean-->
21 <bean id="proxyfactory" class="org.springframework.aop.framework.ProxyFactoryBean">
22 <!--要增强的对象-->
23 <property name="target" ref="service"></property>
24 <!--增强的内容-->
25 <property name="interceptorNames" value="myadvisor"></property>
26 </bean>
27
28</beans>
29
单测方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 1package cn.dawn.day16advisor02;
2
3
4import org.junit.Test;
5import org.springframework.context.ApplicationContext;
6import org.springframework.context.support.ClassPathXmlApplicationContext;
7
8/**
9 * Created by Dawn on 2018/3/3.
10 */
11public class test20180310 {
12 @Test
13 /*aop代理工厂bean异常增强*/
14 public void t01(){
15 ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day16advisor02.xml");
16 SomeServiceImpl service = (SomeServiceImpl) context.getBean("proxyfactory");
17
18 service.doSome();
19 service.doAny();
20
21 }
22}
23