首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

BeanFactory的对象登记与管理方式

2012-11-18 
BeanFactory的对象注册与管理方式1、硬编码方式:public static void main(String[] args){ DefaultListable

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;}}


2、属性文件方式
//加载属性文件的方法
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

3、xml的配置文件方式
加载类
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"/>

4、注解方式(略)
来自spring攻略  王福强著

热点排行