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 ...
随机推荐
- js中forEach,for in,for of循环的用法详解
一.一般的遍历数组的方法: var array = [1,2,3,4,5,6,7]; for (var i = 0; i < array.length; i) { console.log(i,a ...
- Linux进阶之Shell编程
1.什么是Shell Shell是一个命名行解释器,它为用户提供一个向Linux内核发送请求以便运行程序的界面系统级程序,用户可以用Shell来启动.挂起.停止甚至是编写一些程序. 2.Shell脚本 ...
- rust 函数的使用
fn main() { println!("Hello, world!"); another_function(2,3); let y ={ let x =3; //表达式的结尾没 ...
- var a = function(){}和var a = function(){}();的区别
var a = function(){ ... ... ... } 声明方法. var a = function(){ ... ... ... }(); 声明方法并执行 demo: var u = f ...
- TICK/TIGK运维栈安装运行【上】
TICK/TIGK运运维metrics栈包括 InfuluxDB:为时间序列数据专门定制的高性能数据存储.TSM引擎允许高速接收和数据压缩.完全go语言编写.编译成一个单一的二进制,没有外部依赖.简单 ...
- SDM439平台出现部分机型SD卡不能识别mmc1: error -110 whilst initialising SD card【学习笔记】
SDM439平台出现部分机型SD卡不能识别mmc1: error -110 whilst initialising SD card 打印了如下的log: - ::>[ after ms - :: ...
- Java_jdbc 基础笔记之十三 数据库连接(DAO)
public class DAO { // INSERT, UPDATE, DELETE 操作都可以包含在其中 public void update(String sql, Object... arg ...
- 秒杀功能压测 jmeter----------windows系统运行jmeter遇到的坑
最近做了一个安全传输模块,因为怕对性能有较大影响,因此测试安排了针对性的压测 压测的过程出现了一点小问题 发现失败率特别高,测试怀疑是服务端出了错,但是我查看日志发现没有报错.后面我观察TCP链接数排 ...
- Windows10纯净原版系统安装方法
原文:https://baijiahao.baidu.com/s?id=1626696368732676440&wfr=spider&for=pc 1.先来下载一个纯净的ISO系统安装 ...
- 微信小程序开发——修改小程序原生checkbox、radio默认样式
复选框: 闲话少说,这里直接介绍如何修改小程序提供的复选框的样式,如原生的是这样的: 需要的是这样的: 示例代码: /*复选框外框样式*/ checkbox .wx-checkbox-input { ...