这个是applicationContext.xml的配置文件,如何做到修改b002中b001这个属性。
<bean name="b002" class="com.alibaba.zj.spring.bean.B002" init-method="init"> <property name="name" value="bbbbbbbbbbbbb" /> <property name="b001" ref="b001"/> </bean>
增加一个FactoryBean的自定义类。
<bean name="b001FactoryBean" class="com.alibaba.zj.spring.factorybean.B001FactoryBean"></bean>
java代码:如下:
package com.alibaba.zj.spring.factorybean; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.InitializingBean; import com.alibaba.zj.spring.bean.B001; import com.alibaba.zj.spring.bean.B002; public class B001FactoryBean implements FactoryBean ,InitializingBean,BeanFactoryAware { private BeanFactory beanFactory; private B001 b001; @Override public Object getObject() throws Exception { return b001; } @Override public Class getObjectType() { return B001.class; } @Override public boolean isSingleton() { return true; } public B001 getB001() { return b001; } @Override public void afterPropertiesSet() throws Exception { this.b001 = new B001(); this.b001.setName("FactoryBean Created me"); } @Override public void setBeanFactory(BeanFactory beanFactory) throws BeansException { this.beanFactory = beanFactory; } public BeanFactory getBeanFactory() { return beanFactory; } }