spring 笔记 (3)-aop

1.前置通知

public interface MethodBeforeAdvice{
    void before(Method method, Object[] args,Object target)throws Throwable;
}

public class WelcomeAdvice implements MethodBeforeAdvice{
    public void before(Method method, Object[] args, Object target){
        Customer customer =(Customer) args[0];
        System.out.println("Hello "+customer.getName() +" .How are you doing?");
    }
}

<beans>
    <bean id="**Target" class="****.**"/>
    <bean id="welcomeAdvice" class="*****.WelcomeAdvice"/>
    <bean id="**" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="proxyInterfaces ">
            <value>interface of the target class</value>
        </property>
        <property name="interceptorNames ">
            <list>
                <value>welcomeAdvice</value>
            </list>
        </property>
        <property name="target ">
            <ref bean="**Target"/>
        </property>
    </bean>
</beans>

 

public interface AfterReturningAdvice{
    void afterReturning(Object returnValue,Method method, Object[] args,Object target)throws Throwable;
}
public class ThankYouAdvice implements AfterReturningAdvice{
    public void afterReturning (Object returnValue, Method method, Object[] arg2,Object target)throws Throwable{
        System.out.println("Thank you.");
    }
}
public interface MethodInterceptor extends Interceptor{
    Object invoke (MethodInvocation invocation )throws Throwable;
}
MethodInterceptor 接口和前面的2种通知不同:它可以控制目标方法是否真的被调用.
通过调用MethodInvocation.proceed()方法来调用.而且,MethodInterceptor可以控制返回的对象.
你可以返回一个与proceed()方法返回对象完全不同的对象.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值