Spring之Aop

通知
1、Around 攔截對目標方法調用
2、Before 在目標方法調用前調用
3、After 在目標方法調用後調用
4、Throws 當目標方法拋出異常時調用

before
代理對象:(proxyFactoryBeans)只需配置,無需寫,使用動態代理技術
編程說明:
1、定義接口
2、編寫對象(被代理對象)
3、編寫通知(前置通知在調用前執行)
4、在beans.xml中配置
這裏寫圖片描述

//前置通知
public class MyMethodBeforeAdvice implements MethodBeforeAdvice {

    /**
     * method: 被調用方法名字
     * args: 給method傳遞的參數
     * target: 目標對象
     */
    public void before(Method method, Object[] args, Object target)
            throws Throwable {
        // TODO Auto-generated method stub
        System.out.println("記錄日誌..."+method.getName());

    }
}
//後置通知
public class MyAfterReturnAdvices implements AfterReturningAdvice{

    @Override
    public void afterReturning(Object arg0, Method method, Object[] args,
            Object target) throws Throwable {
        System.out.println("關閉資源");
    }

}
//環繞通知
public class MyMethodInterceptor implements MethodInterceptor {

    @Override
    public Object invoke(MethodInvocation arg0) throws Throwable {
        System.out.println("環繞通知調用前");
        Object obj=arg0.proceed();
        System.out.println("環繞通知調用後");
        return obj;
    }

}
//異常通知
public class MyThrowsAdvice implements ThrowsAdvice{
    public void afterThrowing(Method method, Object[] args, Object target,Exception e){
        System.out.println("?????????");
        System.out.println("有異常"+e.getMessage());

    }

}
public interface TestServiceInter {

    public void sayHello();
}

1、被代理對象的對象

public class Test1Service implements TestServiceInter,TestServiceInter2 {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void sayHello() {
        System.out.println("hi "+name);
    }

    public void sayBye() {
        System.out.println("bye "+name);
    }

}

beans.xml的內容、
1、被代理對象的對象
2、前置通知
3、代理對象

<?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/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" 
                >
<!-- 配置被代理的對象 -->
<bean id="test1Service" class="com.hsp.aop.Test1Service">
<property name="name" value="順平" />
</bean>
<!-- 配置前置通知 -->
<bean id="MyMethodBeforeAdvice" class="com.hsp.aop.MyMethodBeforeAdvice" /> 
<!-- 配置代理對象 -->
<bean id="proxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean">
</bean>
<!-- 配置後置通知-->
<bean id="myAfterReturnAdvices" class="com.hsp.aop.MyAfterReturnAdvices">
</bean>
<!-- 代理接口集 -->
<property name="proxyInterfaces">
    <list>
        <value>com.hsp.aop.TestServiceInter</value>
        <value>com.hsp.aop.TestServiceInter2</value>
    </list>
</property>
<!-- 把通知織入到代理對象  -->
<property name="interceptorNames">
    <!-- 相當於包MyMethodBeforeAdvice前置通知和代理對象關聯,我們也
    可以把通知看出攔截器,struts2核心攔截器 -->
    <!--<value>MyMethodBeforeAdvice</value> -->
    <list>
    <!-- 織入前置通知  -->
    <value>MyMethodBeforeAdvice</value>
    <!--  織入後置通知 -->
    <value>myAfterReturnAdvices</value>
    <!-- 織入環繞通知  -->
    <value>myMethodInterceptor</value>
    <!-- 織入異常通知  -->
    <value>myThrowsAdvice</value>
    </list>
</property>
<!-- 配置被代理對象,可以指定 -->
<property name="target" ref="test1Service"/>
</bean>
</beans>

擴展:加一個接口,在接口集里加入


public interface TestServiceInter2 {

    public void sayBye();
}

測試

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ApplicationContext ac=new ClassPathXmlApplicationContext("com/hsp/aop/beans.xml");
        TestServiceInter ts=(TestServiceInter) ac.getBean("proxyFactoryBean");
        ts.sayHello();
        ((TestServiceInter2)ts).sayBye();

    }
//引入通知
<!-- 只有sayBye這個方法調用前置通知 -->
<!-- 定義切入點 -->
<bean id="MyMethodBeforeAdviceFilter" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="advice" ref="MyMethodBeforeAdvice"></property>
<property name="mappedNames">
    <list>
    <value>sayBye</value>
    </list>
</property>
</bean>
發佈了31 篇原創文章 · 獲贊 6 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章