spring aop(代理,通知) java的動態代理

 

1java的動態代理

1.1建立一個接口


public interface IPreson {
//吃
    public void eat();
}

1.2創建一個實現類

public class XiaoMing implements  IPreson {
    @Override
    public void eat() {
        System.out.println("hahah");
    }
}

1.3創建代理實現接口(InvocationHandler)

public class Proxy implements InvocationHandler {

    private IPreson pr;

    public Proxy(IPreson pr) {
        this.pr = pr;
    }
//實現InvocationHandler方法
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        return method.invoke(pr,args);
    }
}

1.4測試

public class proxy2 {


    public static void main(String[] args) {
//里氏替換原則
        Ipreson ip = new XiaoMing();
//實例化代理類
        pojo.Proxy proxy = new pojo.Proxy(ip);
//調用動態代理方法
    Ipreson ip= ( Ipreson ) Proxy.newProxyInstance(XiaoMing.class.getClassLoader(), XiaoMing.class.getInterfaces(), proxy);
//實現吃
        ir.eat();

spring代理

1.接口

public interface Iperson {
    public void slip();
    public  void personadd();
}

 2.實現類

    @Override
    public void slip() {
        System.out.println("睡覺!");
    }

3.spring.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       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-4.3.xsd">
<!--配置目標-->
    <bean id="xm" class="Xiaoming"></bean>
    <!--配置混合代理-->
    <bean id="myproxy"  class="org.springframework.aop.framework.ProxyFactoryBean">
        <!--引用目標對象-->
       <property name="target" ref="xm"></property>
        <property name="proxyInterfaces">
            <list>
                <value>Iperson</value>
            </list>
        </property>
    </bean>

</beans>

4.測試

 @Test
    public void Springproxy() {
//加載spring.xml
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
//獲取bean
        Iperson ip = (Iperson) applicationContext.getBean("myproxy");
//實現方法
        ip.slip();
    }

 

 

2通知

前置通知:在方法前調用(可以存放session)實現MethodBeforeAdvice接口

public class mybefore implements MethodBeforeAdvice{
    @Override
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println("使用了前置通知");
    }
}

後置通知:在方法後調用(可以做權限的判斷)實現AfterReturningAdvice接口


public class myafor  implements AfterReturningAdvice{
    @Override
    public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
        System.out.println("我是後置通知");
    }
}

環繞通知:在方法執行前後執行(日誌)實現MethodInterceptor接口

public class Hrtongzhi implements MethodInterceptor{
    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        System.out.println("環繞通知");
        return methodInvocation.proceed();
    }
}

 

配置spring.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       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-4.3.xsd">
<!--配置目標-->
<!--配置前置通知-->
 <bean id="mybefore" class="mybefore"></bean>
<!--配置通知的過濾-->
    <bean id="advisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
        <property name="advice" ref="mybefore"> </property>
        <property name="pattern" value=".*add.*"></property>
    </bean>
<!--配置後置通知->
    <bean id="afore" class="myafor"></bean>
<!--配置環繞通知-->
    <bean id="hr" class="Hrtongzhi"></bean>
    <!--配置混合代理-->
    <bean id="myproxy"  class="org.springframework.aop.framework.ProxyFactoryBean">
        <!--引用目標對象-->
       <property name="target" ref="xm"></property>
<!--引用接口-->
        <property name="proxyInterfaces">
            <list>
                <value>Iperson</value>
            </list>
        </property>

        <property name="interceptorNames" >
<!--應用通知-->
            <list>
<!--引用過濾前置通知-->
                <idref bean="advisor"></idref>
<!--引用前置通知-->
                <!--<idref bean="mybefore"></idref>-->
<!--引用後置通知-->
                <!--<idref bean="afore"></idref>-->
<!--引用環繞通知-->
                <!--<idref bean="hr"></idref>-->
            </list>
        </property>
</beans>

3測試

 @Test
    public void Springproxy() {
//加載spring.xml
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
//獲取bean
        Iperson ip = (Iperson) applicationContext.getBean("myproxy");
//實現方法
        ip.slip();
//實現方法過濾通知
        ip.presonadd();
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章