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. 使用RedisDesktopManager客户端无法连接Redis服务器问题解决办法

    是否遇到安装完成后连不上的问题? 那么这篇教程能解决. 执行步骤: 1.修改redis文件夹下redis.cong文件,在bind 127.0.0.1行前面加#注释掉这一行,使能远程连接(默认只能使用 ...

  2. 升级pip3的正确姿势--python3 pip3 update

    升级pip3的正确姿势为: pip3 install --upgrade pip 而不是 pip3 install --upgrade pip3

  3. JDK 下载相关资料

    所有版本JDK下载地址: http://www.oracle.com/technetwork/java/archive-139210.html 下载账户密码: 2696671285@qq.com Or ...

  4. element ui,input框输入时enter健进行搜索

    <el-form-item label="企业名称"> <el-input v-model="formSearch.kw" @keyup.en ...

  5. hadoop平台上HDFS和MAPREDUCE的功能、工作原理和工作过程

    作业要求来源:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/3319 1.用自己的话阐明Hadoop平台上HDFS和MapReduce ...

  6. 关于Linux中nohup.out日志过大问题

    背景,java项目,一般在运行JAVA程序时需要用到nohup命令来实现后台启动日志,默认保存在当前目露nohup.out文件.但是有些程序输出nohup文件会出现过大的情况. 在此解决如下: 1,在 ...

  7. openresty开发系列39--nginx+lua实现接口签名安全认证

    一)需求背景现在app客户端请求后台服务是非常常用的请求方式,在我们写开放api接口时如何保证数据的安全,我们先看看有哪些安全性的问题 请求来源(身份)是否合法?请求参数被篡改?请求的唯一性(不可复制 ...

  8. [LeetCode] 544. Output Contest Matches 输出比赛匹配对

    During the NBA playoffs, we always arrange the rather strong team to play with the rather weak team, ...

  9. MySQL之SQL语句的使用

    SQL使用 mysql中的基本逻辑对象 mysql有这么几种对象 mysqld--->库---->表---->记录(由行和列组成)一条记录中的一列叫做字段 什么是关系型数据库 表与表 ...

  10. consul reconnect_timeout

    reconnect_timeout这将控制从集群中彻底删除发生故障的节点需要多长时间.默认值为72小时,建议将其设置为至少为节点或网络分区的预期可恢复的最大停机时间的两倍.警告:将此时间设置得太低可能 ...