Spring关于增强(前置,后置,异常,环绕。。。)

Spring的增强分类:前置增强,后置增强,异常增强,环绕增强。

增强的意义在于我们不改变接口,实现类,不动方法的前提下在原内容上增加内容。

首先我们看一下前置增强:前置增强必须实现的接口:MethodBeforeAdvice

MethodBeforeAdvice

package cn.happy.spring11;

/**
 * Created by linlin on 2017/7/31.
 */
public interface IDoSome {
    public void add();
    public void add1();
    public void add2();


}

package cn.happy.spring11;

/**
 * Created by linlin on 2017/7/31.
 */
public class IDoSomes implements IDoSome{
    public void add() {
        System.out.println("=========1=======");
    }
    public void add1() {
        System.out.println("=========2=======");
    }public void add2() {
        System.out.println("=========3=======");
    }

}

package cn.happy.spring11;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

/**
 * Created by linlin on 2017/7/31.
 */
public class MyBeforeAdvice implements MethodBeforeAdvice{
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println("=========before=======");
    }
}

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd ">
<bean id="Idosome" class="cn.happy.spring11.IDoSomes"></bean>
    <bean id="beforeAdvice" class="cn.happy.spring11.MyBeforeAdvice"></bean>
<bean id="someProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="target" ref="Idosome"></property>
    <property name="interceptorNames" value="beforeAdvice"></property>

</bean>

</beans>

  @Test
    public void test01(){
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext07.xml");
        IDoSome i=(IDoSome)ctx.getBean("someProxy");
        i.add();
        i.add1();
        i.add2();
    }




下面是后置增强:实现的接口是: AfterReturningAdvice
package cn.happy.spring12;

/**
 * Created by linlin on 2017/7/31.
 */
public interface IDoSome {
    public void add();
    public String add1();
    public void add2();


}

package cn.happy.spring12;

/**
 * Created by linlin on 2017/7/31.
 */
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值