读书人

步骤替换MethodReplacer

发布时间: 2012-09-02 21:00:34 作者: rapoo

方法替换MethodReplacer

?

?

package com.astute.sparrow.spring.ioc.method_injection;import org.springframework.beans.BeansException;import org.springframework.beans.factory.BeanFactory;import org.springframework.beans.factory.BeanFactoryAware;import org.springframework.beans.factory.ObjectFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.stereotype.Component;@Component("a")public class A implements BeanFactoryAware {@Autowired@Qualifier("b")private B b;private BeanFactory factory;private ObjectFactory objectFactory;public void printB() {System.out.println(getB());}public void setObjectFactory(ObjectFactory objectFactory) {this.objectFactory = objectFactory;}public B getB() {//return (B) factory.getBean("b");//return (B) objectFactory.getObject();return b;}public void setB(B b) {this.b = b;}@Overridepublic void setBeanFactory(BeanFactory beanFactory) throws BeansException {this.factory = beanFactory;}}package com.astute.sparrow.spring.ioc.method_injection;import org.springframework.context.annotation.Scope;import org.springframework.stereotype.Component;@Component("b")@Scope("prototype")public class B {}
?
<bean id="b" scope="prototype"/><bean id="a" ref="b"/><replaced-method name="printB" replacer="aMethodReplacer"/></bean><bean id="aMethodReplacer" name="code">BeanFactory factory = new XmlBeanFactory(new ClassPathResource("com/astute/sparrow/spring/ioc/method_injection/spring-methodreplacer.xml"));A a = (A) factory.getBean("a");a.printB();a.printB();a.printB();

?

输出:

写道wo do nothing this time...
wo do nothing this time...
wo do nothing this time...?

?

?

?

?

读书人网 >软件架构设计

热点推荐