DefaultAdvisorAutoProxyCreator

来源:互联网 发布:nginx rtmp 延时配置 编辑:程序博客网 时间:2024/04/30 13:17
 
DefaultAdvisorAutoProxyCreator
2008年04月25日 星期五 08:35
DefaultAdvisorAutoProxyCreator
-----------------------------------------------------------------------------------------------------------
package AutoProxyTwo;
public class Customer {
    private String name;
    private String id;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public Customer(String name, String id) {
        super();
        this.name = name;
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

-----------------------------------------------------------------------------------------------------------
package AutoProxyTwo;
import StaticAdvisorTest.Shopping;
public class ShoppingImplA implements Shopping {
    private Customer customer;
    public Customer getCustomer() {
        return customer;
    }
    public void setCustomer(Customer customer) {
        this.customer = customer;
    }
    public String buySomething(String type) {
        System.out.println(this.getCustomer().getName()
                + this.getCustomer().getId() + " 买 " + type + " success");
        return null;
    }
    public String buyAnything(String type) {
        System.out.println(this.getCustomer().getName() + " 买 " + type
                + " success");
        return null;
    }
    public String sellAnything(String type) {
        System.out.println(this.getCustomer().getName() + " sell " + type
                + " success");
        return null;
    }
    public String sellSomething(String type) {
        System.out.println(this.getCustomer().getName() + " sell " + type
                + " success");
        return null;
    }
}

-----------------------------------------------------------------------------------------------------------
package AutoProxyTwo;

import StaticAdvisorTest.Shopping;

public class ShoppingImplB implements Shopping {
    private Customer customer;
    public Customer getCustomer() {
        return customer;
    }
    public void setCustomer(Customer customer) {
        this.customer = customer;
    }
    public String buySomething(String type) {
        System.out.println(this.getCustomer().getName()+" bye "+type+" success");
        return null;
    }
   
    public String buyAnything(String type) {
       System.out.println(this.getCustomer().getName()+" bye "+type+" success");
       return null;

     }
    public String sellAnything(String type) {
        System.out.println(this.getCustomer().getName()+" sell "+type+" success");
        return null;
    }
    public String sellSomething(String type) {
         System.out.println(this.getCustomer().getName()+" sell "+type+" success");
           return null;
    }

}
-----------------------------------------------------------------------------------------------------------
package AutoProxyTwo;
import java.io.File;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;
import StaticAdvisorTest.Shopping;
public class TestAdvisor {
    public static void main(String[] args) {
        String filePath = System.getProperty("user.dir") + File.separator
                + "src" + File.separator + "applicationContext.xml";
        System.out.println(filePath);
        BeanFactory factory = new XmlBeanFactory(new FileSystemResource(
                filePath));
        ApplicationContext ctx = new FileSystemXmlApplicationContext(filePath);
        Shopping shoppingA = null;
        Shopping shoppingB = null;
        shoppingA = (Shopping) ctx.getBean("buyBean");
        shoppingB = (Shopping) ctx.getBean("sellBean");
        shoppingA.buySomething("something");
        shoppingA.buyAnything("anything");
        shoppingB.sellAnything("anything");
        shoppingB.sellSomething("something");
    }
}



-----------------------------------------------------------------------------------------------------------
package AutoProxyTwo;

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;
//前置通知
public class WelcomeAdvice implements MethodBeforeAdvice {

    public void before(Method method, Object[] args, Object obj)
            throws Throwable {
       
        System.out.println("Hello welcome to 买 ");

    }

}
-----------------------------------------------------------------------------------------------------------
package StaticAdvisorTest;

public interface Shopping{
    public String buySomething(String type);
    public String buyAnything(String type);
    public String sellSomething(String type);
    public String sellAnything(String type);

    }


-----------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<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-2.0.xsd">

    <bean ></bean>

    <!-- 自动代理所有的advisor -->
    <bean
        >
    </bean>


    <bean
        >
        <property >
            <value>.*sell.+</value><!-- 业务实现方法名匹配 -->
        </property>
        <property >
            <ref bean="WelcomeAdvice" />
        </property>
    </bean>


    <bean >
        <property >
            <ref bean="customer" />
        </property>
    </bean>
    <bean >
        <property >
            <ref bean="customer" />
        </property>
    </bean>


    <bean >
        <constructor-arg index="0">
            <value>熊熊</value>
        </constructor-arg>
        <constructor-arg index="1">
            <value>66</value>
        </constructor-arg>
    </bean>
</beans>
-----------------------------------------------------------------------------------------------------------

本篇日志被作者设置为禁止发表新评论


©2008 Baidu



引文来源  DefaultAdvisorAutoProxyCreator_熊熊之家
原创粉丝点击