Spring-AOP @AspectJ进阶之绑定连接点方法的返回值
【摘要】
文章目录
概述实例
概述
在后置增强中,可以通过returning绑定连接点方法的返回值
实例
代码已托管到Github—> https://github.com/yang...
概述
在后置增强中,可以通过returning绑定连接点方法的返回值
实例
代码已托管到Github—> https://github.com/yangshangwei/SpringMaster
业务类
package com.xgj.aop.spring.advisor.aspectJAdvance.bingReturnValue;
import org.springframework.stereotype.Component;
@Component
public class BussinessRet {
public String dealBussiness() {
System.out.println("dealBussiness executed");
return "SUCCESS";
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
切面
package com.xgj.aop.spring.advisor.aspectJAdvance.bingReturnValue;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
/**
*
*
* @ClassName: BindReturnValueAspect
*
* @Description: @Aspect标注的切面,
*
* 在后置增强中,可以通过returning绑定连接点方法的返回值
*
* (1)处和(2)处的名字必须相同,此外(2)处retMsg的类型必须和连接点方法的返回值类型匹配
*
* @author: Mr.Yang
*
* @date: 2017年9月12日 下午5:47:23
*/
@Aspect
public class BindReturnValueAspect {
// (1)
@AfterReturning(value = "target(com.xgj.aop.spring.advisor.aspectJAdvance.bingReturnValue.BussinessRet)", returning = "retMsg")
public void crossCuttingCode(String retMsg) {// (2)
System.out.println("----bindReturnValue----");
System.out.println("retMsg:" + retMsg);
System.out.println("----bindReturnValue----");
}
}
- 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
- 30
- 31
- 32
(1)处和(2)处的名字必须相同,此外(2)处retMsg的类型必须和连接点方法的返回值类型匹配
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- (1)声明Context命名空间以及Schema文件 (2)扫描类包以及应用注解定义的bean -->
<context:component-scan base-package="com.xgj.aop.spring.advisor.aspectJAdvance.bingReturnValue"/>
<!-- 基于@AspectJ切面的驱动器 -->
<aop:aspectj-autoproxy proxy-target-class="true"/>
<!-- 使用了@AspectJ注解的切面类 -->
<bean class="com.xgj.aop.spring.advisor.aspectJAdvance.bingReturnValue.BindReturnValueAspect"/>
</beans>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
测试类
package com.xgj.aop.spring.advisor.aspectJAdvance.bingReturnValue;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class BindReturnValueAspectTest {
@Test
public void test() {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"classpath:com/xgj/aop/spring/advisor/aspectJAdvance/bingReturnValue/conf-bindReturnValue.xml");
BussinessRet bussinessRet = ctx.getBean("bussinessRet",
BussinessRet.class);
bussinessRet.dealBussiness();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
输出结果
2017-09-12 17:53:10,730 INFO [main] (AbstractApplicationContext.java:583) - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@3695de1a: startup date [Tue Sep 12 17:53:10 BOT 2017]; root of context hierarchy
2017-09-12 17:53:10,818 INFO [main] (XmlBeanDefinitionReader.java:317) - Loading XML bean definitions from class path resource [com/xgj/aop/spring/advisor/aspectJAdvance/bingReturnValue/conf-bindReturnValue.xml]
dealBussiness executed
----bindReturnValue----
retMsg:SUCCESS
----bindReturnValue----
- 1
- 2
- 3
- 4
- 5
- 6
- 7
文章来源: artisan.blog.csdn.net,作者:小小工匠,版权归原作者所有,如需转载,请联系作者。
原文链接:artisan.blog.csdn.net/article/details/77961249
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)