BeanFactory的对象注册与管理方式
1、硬编码方式:
public static void main(String[] args){ DefaultListableBeanFactory beanregistry=new DefaultListableBeanFactory(); BeanFactory container=(BeanFactory)bindViaCode(beanRegistry); FXNewsProvider newsProvider=(FXNewsProvider)container.getBean("djNewsProvider"); newsProvider.getAndPersistNews();}public static BeanFactory bindViaCode(BeanDefinitionRegistry registry){ AbstractBeanDefinition newsProvider=new RootBeanDefintion(FXNewsProvider.class,true);AbstractBeanDefinition newsListener=new RootBeanDefintion(DowJonesNewListerner.class,true);AbstractBeanDefinition newsPersister=new RootBeanDefintion(DowJonesNewPersister.class,true);//将bean定义到注册容器中registry.registerBeanDefinition("djNewsProvider",newsProvider);registry.registerBeanDefinition("djListener",newsListener);registry.registerBeanDefinition("djPersister",newsPersister);//指定依赖关系//通过构造函数方式注入ConstructorArgumentValues argValues=new ConstructorArgumentValues();argValues.addIndexedArgumentValue(0,newsListener);argValues.addIndexedArgumentValue(1,newsPresister);newsProvier.setConstructorArgumentValues(argValues);//通过setter方式注入MutablepropertyValues propertyValues=new MutablePropertyValues();propertyValues.addPropertyValue(new ropertyValue("newsListener",newsListener));propertyValues.addPropertyValue(new ropertyValue("newsPresister",newsPresister));newsProvider.setPropertyValues(propertyValues);//绑定完成return (BeanFactory)registry;}}
import org.springframework.beans.factory.support.BeanDefinitionRegistry;import org.springframework.beans.factory.support.DefaultListableBeanFactory;import org.springframework.beans.factory.support.PropertiesBeanDefinitionReader;import org.springframework.beans.factory.BeanFactory;public class BeanFactoryProperties {public static void main(String[] args){DefaultListableBeanFactory beanRegistry=new DefaultListableBeanFactory();//创建注册依赖的实例BeanFactory container=(BeanFactory)bindViaPropertiesFile(beanRegistry);//调用函数,并将beanRegistry传入函数实现注册,DefaultListableBeanFactory继承自BeanFactory才能转换FXNewsProvider newsProvider=(FXNewsProvider)container.getBean("djNewsProvider");//获得beannewsProvider.getAndPersistNews();//调用方法}public static BeanFactoryProperties bindViaPropertiesFile(BeanDefinitionRegistry registry){PropertiesBeanDefinitionReader reader=new PropertiesBeanDefinitionReader(registry);//创建能够加载属性文件的类的实例reader.loadBeanDefinitions("classpath:../binding-config.properties");//加载属性文件return (BeanFactoryProperties)registry;//返回bean实例}}
djNewsProvider.(class)=bao.FxnewsProvider#通过构造函数注入djNewsProvider.${0}=djListenerdjNewsprovider.${1}=djPersister#通过setter方法注入#djNewsprovider.newsLister(ref)=djListener#djNewsProvider.newPersistener(ref)=djPersisterdjLister.(class)=..DowListenerdjPersister.(class)=..DowPersister
import org.springframework.beans.factory.BeanFactory;import org.springframework.beans.factory.support.BeanDefinitionRegistry;import org.springframework.beans.factory.support.DefaultListableBeanFactory;import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;public class XmlBeanFactoryLoader {DefaultListableBeanFactory beanRegistry=new DefaultListableBeanFactory();//创建注册依赖的实例BeanFactory container=(BeanFactory)bindViaXMLFile(beanRegistry);//调用函数,并将beanRegistry传入函数实现注册,DefaultListableBeanFactory继承自BeanFactory才能转换FXNewsProvider newsProvider=(FXNewsProvider)container.getBean("djNewsProvider");//获得beannewsProvider.getAndPersistNews();//调用方法public static BeanFactory bindViaXMLFile(BeanDefinitionRegistry registry){XmlBeanDefinitionReader reader=new XmlBeanDefinitionReader(registry);reader.loadBeanDefinitions("classpath:../applicationContext.xml");return (BeanFactory)registry;//或者直接//return new XmlBeanFactory(new ClassPathResource("/applicationContext");}}
<bean id="djNewsProvider" /></constructor-arg></bean><bean id="djNewsListener" class="..DowPersister"/>