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实例的更多相关文章

  1. Spring AOP创建Throwdvice实例

    1.异常发生的时候,通知某个服务对象做处理 2.实现throwsAdvice接口 接口实现: public interface IHello { public void sayHello(String ...

  2. spring AOP的注解实例

    上一篇写了spring AOP 的两种代理,这里开始AOP的实现了,个人喜欢用注解方式,原因是相对于XML方式注解方式更灵活,更强大,更可扩展.所以XML方式的AOP实现就被我抛弃了. 实现Sprin ...

  3. 阿里四面:你知道Spring AOP创建Proxy的过程吗?

    Spring在程序运行期,就能帮助我们把切面中的代码织入Bean的方法内,让开发者能无感知地在容器对象方法前后随心添加相应处理逻辑,所以AOP其实就是个代理模式. 但凡是代理,由于代码不可直接阅读,也 ...

  4. Spring学习(十八)----- Spring AOP+AspectJ注解实例

    我们将向你展示如何将AspectJ注解集成到Spring AOP框架.在这个Spring AOP+ AspectJ 示例中,让您轻松实现拦截方法. 常见AspectJ的注解: @Before – 方法 ...

  5. Spring AOP创建BeforeAdvice和AfterAdvice实例

    BeforeAdvice 1.会在目标对象的方法执行之前被调用. 2.通过实现MethodBeforeAdvice接口来实现. 3.该接口中定义了一个方法即before方法,before方法会在目标对 ...

  6. Spring AOP 创建增强类

    AOP联盟为增强定义了org.aopalliance.aop.Advice接口,Spring支持5种类型的增强:     1)前置增强:org.springframework.aop.BeforeAd ...

  7. Spring AOP 创建切面

        增强被织入到目标类的所有方法中,但是如果需要有选择性的织入到目标类某些特定的方法中时,就需要使用切点进行目标连接点的定位.增强提供了连接点方位信息:如织入到方法前面.后面等,而切点进一步描述织 ...

  8. spring aop简单日志实例

    转载自:http://www.blogjava.net/laoding/articles/242611.html 一直就用spring的IOC,遗憾spring的另一重要组成部分AOP却没用过,所以近 ...

  9. Spring AOP前置通知实例说明AOP相关概念

    今天又看了下韩顺平的SpringAOP的讲解,讲解的很透彻.仿照视频自己使用下前置通知. 一.引出问题 有个接口TestServiceInter,有两个实现方法TestService和Test2Ser ...

随机推荐

  1. [golang]Go常见问题:# command-line-arguments: ***: undefined: ***

    今天遇见一个很蛋疼的问题,不知道是不是我配置的问题,IDE直接run就报错. 问题描述 在开发代码过程中,经常会因为逻辑处理而对代码进行分类,放进不同的文件里面:像这样,同一个包下的两个文件,点击id ...

  2. 基于Linux(中标麒麟)上QT的环境搭建——解决cannot find lGL的问题

    接上一篇,QT在中标麒麟环境安装完成后遇到运行报错的问题 一.问题描述: 在中标麒麟上配置好QT的环境后,新建一个工程,不做其他的任何操作,直接运行都会报cannot find lGL的错误.如图所示 ...

  3. Go内置函数

    append go语言中的append函数作用是在切片变量的后面追加新的数据,然后返回新的切片变量 func append(slice []Type, elems ...Type) []type sl ...

  4. 前端base64加密

    一.Base64编码表 码值 字符 码值 字符 码值 字符 码值 字符 0 A 16 Q 32 g 48 w 1 B 17 R 33 h 49 x 2 C 18 S 34 i 50 y 3 D 19 ...

  5. TOMCAT 可以稳定支持的最大并发用户数

      微软系统平台上 TOMCAT性能调优后可以稳定支持的最大并发用户数量在300人服务器配置:   单硬盘,SATA 8MB缓存测试服务器和loadrunner运行服务器位于同一网段 100MB网络( ...

  6. windows环境下安装nginx

    (1)下载 官网:http://nginx.org/en/docs/windows.html 下载:http://nginx.org/en/docs/windows.html 点击:nginx/Win ...

  7. GD-GAN: Generative Adversarial Networks for Trajectory Prediction and Group Detection in Crowds

    GD-GAN: Generative Adversarial Networks for Trajectory Prediction and Group Detection in Crowds 2019 ...

  8. linux shell中如何批量添加一行内容到某些文件的末尾?

    答:先使用find找出要指定的某些文件,然后使用xargs和sed工具将内容插入到这些文件的末尾 find . -name 'filename*' | xargs sed -i '$a\added-c ...

  9. react ui 参考网站

    react.semantic-ui.com react官方中文网址 https://zh-hans.reactjs.org/ es6标准入门 nodejs官网文档 nodejs.org https:/ ...

  10. RedisHelper Redis帮助类

    using StackExchange.Redis; using System; using System.Collections.Generic; using System.IO; using Sy ...