AOP快速入门案例(三)

一 通知类型

二 后置通知
package com.hsp.aop;
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
public class MyAfterReturningAdvice implements AfterReturningAdvice {
    @Override
    public void afterReturning(Object arg0, Method arg1, Object[] arg2,
            Object arg3) throws Throwable {
        // TODO Auto-generated method stub
        System.out.println("关闭资源...");
    }
}

三 环绕通知
package com.hsp.aop;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class MyMethodInterceptor implements MethodInterceptor {
    @Override
    public Object invoke(MethodInvocation arg0) throws Throwable {
        // TODO Auto-generated method stub
        System.out.println("调用方法前");
        Object  obj = arg0.proceed();
        System.out.println("调用方法后");
        return obj;
    }
}

四 异常通知
package com.hsp.aop;
import java.lang.reflect.Method;
import org.springframework.aop.ThrowsAdvice;
public class MyThrowsAdvice implements ThrowsAdvice {
        public void afterThrowing(Method m,Object[] os,Object target,Exception e)
        {
                System.out.println("出大事了"+e.getMessage());
        }
}

五 配置文件
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans";
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
                xmlns:context="http://www.springframework.org/schema/context";
                xmlns:tx="http://www.springframework.org/schema/tx";
                xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd";
                                >
<!-- 配置被代理的对象 -->
<bean id="test1Service" class="com.hsp.aop.Test1Service">
<property name="name" value="顺平" />
</bean>
<!-- 配置前置通知
        proxyFactoryBean implements TestServiceInter,TestServiceInter2{
                public void sayHello();
        }
        
        思考
        interface Inter1{};
        class A implements Inter1,Inter2{
        }
        Inter1 a=new A();
        Inter2 b=(Inter2)a;
-->
<bean id="MyMethodBeforeAdvice" class="com.hsp.aop.MyMethodBeforeAdvice" />
<bean id="myAfterReturningAdvice" class="com.hsp.aop.MyAfterReturningAdvice" />
<bean id="myMethodInterceptor" class="com.hsp.aop.MyMethodInterceptor" />
<bean id="myThrowsAdvice" class="com.hsp.aop.MyThrowsAdvice "/>
<!-- 配置代理对象 -->
<bean id="proxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- 代理接口集 -->
<property name="proxyInterfaces">
        <list>
                <value>com.hsp.aop.TestServiceInter</value>
                <value>com.hsp.aop.TestServiceInter2</value>
        </list>
</property>
<!-- 把通知织入到代理对象  -->
<property name="interceptorNames">
        <!-- 相当于包MyMethodBeforeAdvice前置通知和代理对象关联,我们也
        可以把通知看出拦截器,struts2核心拦截器 -->
        <list>
                <value>MyMethodBeforeAdvice</value>
                <value>myAfterReturningAdvice</value>
                <value>myMethodInterceptor</value>
                <value>myThrowsAdvice</value>
        </list>
</property>
<!-- 配置被代理对象,可以指定 -->
<property name="target" ref="test1Service"/>
</bean>
</beans>

六 运行结果
记录日志...sayHello
调用方法前
hi 顺平
调用方法后
关闭资源...
记录日志...sayBye
调用方法前
bye 顺平
调用方法后
关闭资源...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值