快速上手Spring--10. 任意方法的替换[2]

[入库:2006年2月23日] [更新:2007年3月24日]

本文简介:

 
beans.xml
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
    <bean id="helloReplacer" class="javamxj.spring.basic.MethodReplacer.HelloReplacer"/>
   
    <bean id="helloA" class="javamxj.spring.basic.MethodReplacer.HelloImpl"/>
   
    <bean id="helloB" class="javamxj.spring.basic.MethodReplacer.HelloImpl">
        <replaced-method name="sayHello" replacer="helloReplacer"/>
    </bean>
   
</beans>
 
Main.java
package javamxj.spring.basic.MethodReplacer;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class Main {
    public static void main(String[] args) {
        Resource res = new ClassPathResource(
                "javamxj/spring/basic/MethodReplacer/beans.xml");
        BeanFactory ft = new XmlBeanFactory(res);

        // 没有使用replaced-method
        Hello h = (Hello) ft.getBean("helloA");
        h.sayHello("分享Java快乐");
       
        // 使用replaced-method
        h=(Hello) ft.getBean("helloB");
        h.sayHello("分享Java快乐");

    }
}

本文关键:快速上手Spring--10. 任意方法的替换
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top