您的位置:首页 > 编程语言 > Java开发

Spring与AOP_名称匹配方法切入点顾问(Advisor)

2020-07-14 05:24 441 查看

顾问(Advisor)

介绍

<!-- 注册切面:顾问(Advisor) -->
<bean id="myAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="advice" ref="myAdvice"></property>
<!-- 指定切入点 -->
<!-- <property name="mappedName" value="doFirst"></property> -->
<!-- <property name="mappedNames" value="doFirst,doSecond"></property> -->
<property name="mappedName" value="*ir*"></property>
</bean>

代码

ISomeService.java

package com.study.aop09;

// 主业务接口
public interface ISomeService {
// 目标方法
void doFirst();
// 目标方法
String doSecond();
// 目标方法
void doThird();
}

MyAfterReturningAdvice.java

package com.study.aop09;

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.aop09;

import org.junit.Test;
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/aop09/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.aop09;

// 目标类
public class SomeServiceImpl implements ISomeService {

@Override
public void doFirst() {
System.out.println("执行doFirst()方法");
}

@Override
public String doSecond() {
System.out.println("执行doFirst()方法");
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.aop09.SomeServiceImpl"></bean>

<!-- 注册切面:通知(advice) -->
<bean id="myAdvice" class="com.study.aop09.MyAfterReturningAdvice"></bean>
<!-- 注册切面:顾问(Advisor) -->
<bean id="myAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="advice" ref="myAdvice"></property>
<!-- 指定切入点 -->
<!-- <property name="mappedName" value="doFirst"></property> -->
<!-- <property name="mappedNames" value="doFirst,doSecond"></property> -->
<property name="mappedName" value="*ir*"></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>

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: