方法替换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;
	}

	@Override
	public 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" class="com.astute.sparrow.spring.ioc.method_injection.B" scope="prototype"/>
<bean id="a" class="com.astute.sparrow.spring.ioc.method_injection.A">
	<property name="b" ref="b"/>
	<replaced-method name="printB" replacer="aMethodReplacer"/>
</bean>
<bean id="aMethodReplacer" class="com.astute.sparrow.spring.ioc.method_injection.AMethodReplacer"/>
 
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...
 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值