Spring AOP创建AroundAdvice实例
AroundAdvice
1、在方法之前和之后来执行相应的操作
2、实现MethodInterceptor接口
接口文件:
public interface IHello {
public void sayHello(String str);
}
public class Hello implements IHello {
@Override
public void sayHello(String str) {
System.out.println("你好"+str);
}
}
SayAroundAdvice文件:
public class SayAroundAdvice implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation arg0) throws Throwable {
// TODO Auto-generated method stub
Object result=null;
System.out.println("Around在方法执行前做事情!");
result=arg0.proceed();
System.out.println("Around在方法执行后做事情!");
return result;
}
}
Main文件:
public class MainTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
IHello hello=(IHello)context.getBean("helloProxy");
hello.sayHello("访客");
}
}
applicationContext.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<!-- 建立目标对象实例 -->
<bean id="bean_hello" class="com.pb.Hello" />
<!-- 创建Around advice实例 -->
<bean id="ssd" class="com.pb.SayAroundAdvice" />
<!-- 建立代理对象 -->
<bean id="helloProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- 设置代理的接口 -->
<property name="proxyInterfaces">
<value>com.pb.IHello</value>
</property>
<!-- 设置目标对象实例 -->
<property name="target">
<ref bean="bean_hello"/>
</property>
<!-- 设置Advice实例 -->
<property name="interceptorNames">
<list>
<value>ssd</value>
</list>
</property>
</bean>
</beans>
执行效果:

Spring AOP创建AroundAdvice实例的更多相关文章
- Spring AOP创建Throwdvice实例
1.异常发生的时候,通知某个服务对象做处理 2.实现throwsAdvice接口 接口实现: public interface IHello { public void sayHello(String ...
- spring AOP的注解实例
上一篇写了spring AOP 的两种代理,这里开始AOP的实现了,个人喜欢用注解方式,原因是相对于XML方式注解方式更灵活,更强大,更可扩展.所以XML方式的AOP实现就被我抛弃了. 实现Sprin ...
- 阿里四面:你知道Spring AOP创建Proxy的过程吗?
Spring在程序运行期,就能帮助我们把切面中的代码织入Bean的方法内,让开发者能无感知地在容器对象方法前后随心添加相应处理逻辑,所以AOP其实就是个代理模式. 但凡是代理,由于代码不可直接阅读,也 ...
- Spring学习(十八)----- Spring AOP+AspectJ注解实例
我们将向你展示如何将AspectJ注解集成到Spring AOP框架.在这个Spring AOP+ AspectJ 示例中,让您轻松实现拦截方法. 常见AspectJ的注解: @Before – 方法 ...
- Spring AOP创建BeforeAdvice和AfterAdvice实例
BeforeAdvice 1.会在目标对象的方法执行之前被调用. 2.通过实现MethodBeforeAdvice接口来实现. 3.该接口中定义了一个方法即before方法,before方法会在目标对 ...
- Spring AOP 创建增强类
AOP联盟为增强定义了org.aopalliance.aop.Advice接口,Spring支持5种类型的增强: 1)前置增强:org.springframework.aop.BeforeAd ...
- Spring AOP 创建切面
增强被织入到目标类的所有方法中,但是如果需要有选择性的织入到目标类某些特定的方法中时,就需要使用切点进行目标连接点的定位.增强提供了连接点方位信息:如织入到方法前面.后面等,而切点进一步描述织 ...
- spring aop简单日志实例
转载自:http://www.blogjava.net/laoding/articles/242611.html 一直就用spring的IOC,遗憾spring的另一重要组成部分AOP却没用过,所以近 ...
- Spring AOP前置通知实例说明AOP相关概念
今天又看了下韩顺平的SpringAOP的讲解,讲解的很透彻.仿照视频自己使用下前置通知. 一.引出问题 有个接口TestServiceInter,有两个实现方法TestService和Test2Ser ...
随机推荐
- 地址栏从url输入到页面显示
本文链接:https://blog.csdn.net/MiemieWan/article/details/85708052地址栏输入url, 要通过dns解析(浏览器是不能识别url地址的,需解析成i ...
- SSM项目实战 之 Maven
目录 Maven 简介 Maven是什么 Maven下载安装 Maven使用 Maven规定了一套默认的项目格式 创建第一个Maven项目 Maven仓库 Maven常用命令 Maven作用范围(sc ...
- ES6继承小实例
ES6继承小实例 一.总结 一句话总结: js中的类和继承可以多用es6里面的,和其它后端语言的使用方法一样 class Animal { constructor(name) { this.name ...
- Java线程同步的Monitor机制(Lock配合Condition)
Monitor模式是一种常见的并行开发机制, 一个Monitor实例可以被多个线程安全使用, 所有的monitor下面的方法在运行时是互斥的, 这种互斥机制机制可以用于一些特性, 例如让线程等待某种条 ...
- excel矩阵运算操作-转置 行列式 相乘 逆阵
excel矩阵运算操作-转置 行列式 相乘 逆阵 https://jingyan.baidu.com/article/154b463128e13928ca8f41a4.html Excel中矩阵的相关 ...
- centos6.10中部署percona-mysql双实例的方法
centos6.10中部署percona-mysql双实例的方法 数据库的同步关系:192.168.11.53(master) --> slave(192.168.10.189) --> ...
- SDN实验---使用git安装Mininet
0:补充Ubuntu截屏 截全屏 printscreen 截取当前窗口 alt + printscreen 截取任意矩形 shift + printscreen 截取全屏到剪切板 ctrl + pri ...
- springboot 整合rabbitMQ
RabbitMQ RabbitMQ是一个被广泛使用的开源消息队列.它是轻量级且易于部署的,它能支持多种消息协议.RabbitMQ可以部署在分布式和联合配置中,以满足高规模.高可用性的需求. 在pom. ...
- Create C/C++ DLLs in Visual Studio
https://docs.microsoft.com/zh-tw/cpp/build/dlls-in-visual-cpp?view=vs-2019 Walkthrough: Create and u ...
- word 转化为PDF 目录没了
https://jingyan.baidu.com/article/ca00d56c5d5f8de99febcf54.html 点文件-->导出-->创建PDF-->选项--> ...