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");
	//获得bean
	newsProvider.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}=djListener
djNewsprovider.${1}=djPersister
#通过setter方法注入
#djNewsprovider.newsLister(ref)=djListener
#djNewsProvider.newPersistener(ref)=djPersister
djLister.(class)=..DowListener
djPersister.(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");
	//获得bean
	newsProvider.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" class="FXNewsProvider">
<constructor-arg index="0">
<ref bean="djNewsListener"/>
</constructor-arg>
<constructor-arg index="1">
<ref bean="djNewsPersister" />
</constructor-arg>
</bean>
<bean id="djNewsListener" class="..DowListener">
</bean>
<bean id="djNewsPersister" class="..DowPersister"/>

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

你可能感兴趣的:(spring,bean,xml,配置管理)