BeforeAdvice

1、会在目标对象的方法执行之前被调用。

2、通过实现MethodBeforeAdvice接口来实现。

3、该接口中定义了一个方法即before方法,before方法会在目标对象target之前执行。

AfterAdvice

1、在目标对象的方法执行之后被调用

2、通过实现AfterReturningAdvice接口实现

实现目标:

在方法之前调用执行某个动作。

IHello 和Hello:

public interface IHello {
public void sayHello(String str);
}
public class Hello implements IHello {
@Override
public void sayHello(String str) {
System.out.println("你好"+str);
}
}

SayBeforeAdvice:

public class SayBeforeAdvice implements MethodBeforeAdvice {

	@Override
public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
// TODO Auto-generated method stub
System.out.println("在方法执行前做事情!");
} }

SayAfterAdvice文件:

public class SayAfterAdvice implements AfterReturningAdvice {

	@Override
public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {
// TODO Auto-generated method stub
System.out.println("在方法执行后做事情!");
} }

  

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" />
<!-- 创建执行前advice实例 -->
<bean id="sba" class="com.pb.SayBeforeAdvice" />
<!-- 创建执行后advice实例 -->
<bean id="sfa" class="com.pb.SayAfterAdvice" />
<!-- 建立代理对象 -->
<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>sba</value>
<value>sfa</value>
</list>
</property>
</bean>
</beans>

  

Main执行:

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("访客");
}

  

执行效果:

Spring AOP创建BeforeAdvice和AfterAdvice实例的更多相关文章

  1. Spring AOP创建Throwdvice实例

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

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

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

  3. Spring AOP创建AroundAdvice实例

    AroundAdvice 1.在方法之前和之后来执行相应的操作 2.实现MethodInterceptor接口 接口文件: public interface IHello { public void ...

  4. Spring AOP 创建增强类

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

  5. Spring AOP 创建切面

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

  6. 基于注解的Spring AOP入门、增强Advice实例

    这篇文章简单通过一个例子,介绍几种增强的基本配置,以方便spring框架初学者对aop的代码结构有个清楚的了解认识.首先,spring支持aop编程,支持aspectJ的语法格式来表示切入点,切面,增 ...

  7. Spring AOP 创建Advice 定义pointcut、advisor

    前面定义的advice都是直接植入到代理接口的执行之前和之后,或者在异常发生时,事实上,还可以对植入的时机定义的更细. Pointcut定义了advice的应用时机,在Spring中pointcutA ...

  8. Spring AOP 创建Advice 基于Annotation

    public interface IHello { public void sayHello(String str); } public class Hello implements IHello { ...

  9. Spring AOP 源码分析 - 创建代理对象

    1.简介 在上一篇文章中,我分析了 Spring 是如何为目标 bean 筛选合适的通知器的.现在通知器选好了,接下来就要通过代理的方式将通知器(Advisor)所持有的通知(Advice)织入到 b ...

随机推荐

  1. (15)Go错误处理

    1.erro(一般错误) package main import ( "errors" "fmt" ) func div(a, b int) (res int) ...

  2. 一次修复linux的efi引导的集中方法总结记录

    本文通过MetaWeblog自动发布,原文及更新链接:https://extendswind.top/posts/technical/grub_uefi_repair 起因:EFI分区被删除导致引导问 ...

  3. C Primer Plus--结构和其他数据类型(2)

    目录 枚举类型 enumerated type 枚举默认值 为枚举指定值 命名空间 namespace typedef关键字 * () []修饰符 typedef与这三个运算符结合 函数与指针 函数指 ...

  4. Net core学习系列(五)——Net Core应用程序Startup类介绍

    一.Startup 类 ASP.NET Core应用程序需要一个启动类,按照惯例命名为Startup.在主程序的Web Host生成器(WebHostBuilderExtensions)的 UseSt ...

  5. Deep High-Resolution Representation Learning for Human Pose Estimation

    Deep High-Resolution Representation Learning for Human Pose Estimation 2019-08-30 22:05:59 Paper: CV ...

  6. 监控指标 TP99 TP999 含义

    TP=Top Percentile,Top百分数,是一个统计学里的术语,与平均数.中位数都是一类. TP50.TP90和TP99等指标常用于系统性能监控场景,指高于50%.90%.99%等百分线的情况 ...

  7. Nginx系列 | [转]Nginx 上传文件:client_max_body_size 、client_body_buffer_size

    原文:http://php-note.com/article/detail/488 client_max_body_size client_max_body_size 默认 1M,表示 客户端请求服务 ...

  8. nginx支持websocket及websocket部分原理介绍

    nginx支持websocket及websocket部分原理介绍最近ipc通过websocket与server进行通行,经过无法通过nginx进行反向代理,只有直连nodejs端口.而且部署到阿里云用 ...

  9. docker 部署jar包

    docker volume create j1ls /var/lib/docker/volumes/j1/_data 上传jardocker run -it -d --name j1 -v j1:/h ...

  10. (转)SQLAlchemy入门和进阶

    URL:https://zhuanlan.zhihu.com/p/27400862 https://www.cnblogs.com/mrchige/p/6389588.html---SQLAlchem ...