AOP 环绕通知

package com.gqc.aop03;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

//环绕通知可以修改目标方法的返回结果
public class MyMethodInterceptor implements MethodInterceptor {

	@Override
	public Object invoke(MethodInvocation invocation) throws Throwable {
		System.out.println("执行环绕通知:目标方法执行之前");
		//执行目标方法
		Object result = invocation.proceed();
		System.out.println("执行环绕通知:目标方法执行之后");
		if(result!=null){
			result=((String)result).toUpperCase();
		}
		return result ;
	}

}

<?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 id="someService" class="com.gqc.aop03.SomeServiceImpl"/>
	
		<!-- 注册切面:后置通知 -->
		<bean id="myAdvice" class="com.gqc.aop03.MyMethodInterceptor"/>
		
		<!-- 生成代理对象 -->
		<bean id="serviceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
			<!-- 指定目标对象 -->
			<property name="target" ref="someService"/>
			<!-- <property name="targetName" value="someService"/> -->
			<!-- 指定切面 -->
			<property name="interceptorNames" value="myAdvice"/>
		</bean>
				
</beans>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中,使用AOP环绕通知可以通过配置pom文件和编写测试类来实现。首先,在pom文件中添加以下依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> ``` 这个依赖会自动开启自动代理,相当于使用了`@EnableAspectJAutoProxy`注解,因此不需要再手动添加这个注解。\[1\]\[2\] 接下来,可以编写一个使用Spring Boot单元测试的测试类。首先,导入需要的类: ``` import com.ljs.springbootplace.service.MyService; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; ``` 然后,在测试类上添加注解`@RunWith(SpringJUnit4ClassRunner.class)`和`@SpringBootTest(classes = SpringbootplaceApplication.class)`,指定运行的JUnit运行器和Spring Boot的启动类。同时,注入需要测试的服务类: ``` @Autowired private MyService myService; ``` 最后,编写测试方法,调用需要测试的方法: ``` @Test public void test() { String userNameById = myService.getUserNameById_testAspect(1);//测试aop System.out.println(userNameById); } ``` 这样就可以使用Spring Boot的AOP功能进行环绕通知的测试了。\[3\] #### 引用[.reference_title] - *1* [SpringBoot-AOP环绕通知记录日志/鉴权](https://blog.csdn.net/wjg8209/article/details/119675560)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [springboot实现aop环绕通知](https://blog.csdn.net/weixin_43421537/article/details/104778208)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值