Spring与AOP_正则表达式方法切入点顾问
2020-07-14 05:24
447 查看
正则表达式方法切入点Advisor
- 介绍
- 代码
- ISomeService.java
- MyAfterReturningAdvice.java
- MyTest.java
- SomeServiceImpl.java
- applicationContext.xml
介绍
<!-- 注册切面:顾问(Advisor) --> <bean id="myAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <property name="advice" ref="myAdvice"></property> <!-- 指定切入点: 这里匹配的对象是简单方法名 --> <!-- <property name="mappedName" value="doFirst"></property> --> <!-- <property name="mappedNames" value="doFirst,doSecond"></property> --> <!-- 这里的正则表达式匹配的是全限定性方法名 --> <!-- <property name="pattern" value=".*doFirst"></property> --> <!-- <property name="patterns" value=".*doFirst,.*doSecond"></property> --> <!-- <property name="pattern" value=".*doFirst|.*doSecond"></property> --> <property name="pattern" value=".*S.*"></property> </bean>
代码
ISomeService.java
package com.study.aop10; // 主业务接口 public interface ISomeService { // 目标方法 com.study.aop10.ISomeService.doFirst void doFirst(); // 目标方法 String doSecond(); // 目标方法 void doThird(); }
MyAfterReturningAdvice.java
package com.study.aop10; import java.lang.reflect.Method; import org.springframework.aop.AfterReturningAdvice; // 后置通知,可以获取到目标方法的返回结果,但无法改变目标方法的结果 public class MyAfterReturningAdvice implements AfterReturningAdvice { /** * 在目标方法执行之后执行 * returnValue 目标方法的返回值 */ @Override public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable { System.out.println("执行后置通知方法 returnValue = " + returnValue); } }
MyTest.java
package com.study.aop10; import org.junit.Test; import org.springframework.aop.support.RegexpMethodPointcutAdvisor; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyTest { /** * 通过junit中注解的方式@Test运行函数 */ @Test public void test01() { // 创建容器对象, 加载Spring配置文件 // 会从类路径下查找配置文件 String resource = "com/study/aop10/applicationContext.xml"; ApplicationContext ac = new ClassPathXmlApplicationContext(resource);//ClassPathXmlApplicationContext(); ISomeService service = (ISomeService)ac.getBean("serviceProxy"); service.doFirst(); System.out.println("========================"); String result = service.doSecond(); System.out.println("========================"); service.doThird(); } }
SomeServiceImpl.java
package com.study.aop10; // 目标类 public class SomeServiceImpl implements ISomeService { @Override public void doFirst() { System.out.println("执行doFirst()方法"); } @Override public String doSecond() { System.out.println("执行doSecond()方法"); return "abcde"; } @Override public void doThird() { System.out.println("执行doThrid()方法"); } }
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- bean definitions here --> <!-- 注册目标对象service --> <bean id="someService" class="com.study.aop10.SomeServiceImpl"></bean> <!-- 注册切面:通知(advice) --> <bean id="myAdvice" class="com.study.aop10.MyAfterReturningAdvice"></bean> <!-- 注册切面:顾问(Advisor) --> <bean id="myAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <property name="advice" ref="myAdvice"></property> <!-- 指定切入点: 这里匹配的对象是简单方法名 --> <!-- <property name="mappedName" value="doFirst"></property> --> <!-- <property name="mappedNames" value="doFirst,doSecond"></property> --> <!-- 这里的正则表达式匹配的是全限定性方法名 --> <!-- <property name="pattern" value=".*doFirst"></property> --> <!-- <property name="patterns" value=".*doFirst,.*doSecond"></property> --> <!-- <property name="pattern" value=".*doFirst|.*doSecond"></property> --> <property name="pattern" value=".*S.*"></property> </bean> <!-- 生成代理对象 --> <bean id="serviceProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="someService"></property> <property name="interceptorNames" value="myAdvisor"></property> </bean> </beans>
相关文章推荐
- Spring AOP-顾问-正则表达式方法切入点顾问
- JavaWeb学习笔记-spring-14-AOP-创建切面(静态正则表达式方法匹配切面)
- SSM-Spring-13:Spring中RegexpMethodPointcutAdvisor正则方法切入点顾问
- [原创]java WEB学习笔记105:Spring学习---AOP介绍,相关概念,使用AOP,利用 方法签名 编写 AspectJ 切入点表达式
- Spring-AOP 静态正则表达式方法匹配切面
- spring 注解aop切入点表达式怎么排除某些方法
- spring基于xml配置AOP,的4种常用 切入点表达式的 步骤写法
- AOP-顾问-方法名匹配切入点顾问
- spring AOP配置中的正则表达式(转)
- Spring AOP execution 切入点表达式
- spring(AOP)案例、切入点表达式、aop执行原理
- 框架 day37 Spring3,AOP,代理模式(动态/CGLIB/工厂bean),传统AOP,AspectJ框架(基于xml/注解),切入点表达式,jdbcTemplate
- Spring Aop配置时的切入点表达式
- 关于Spring AOP切入点常用表达式的记录
- Spring AOP execution 切入点表达式
- Spring AOP 切入点表达式增强
- Spring Aop配置时的切入点表达式
- spring AOP切入点表达式规则
- Spring Aop(九)——基于正则表达式的Pointcut
- Spring Aop配置时的切入点表达式