前提背景
public class FXNewsProvider {
private IFXNewsListener newsListener;
private IFXNewsPersister newsPersister;
public FXNewsProvider(IFXNewsListener newsListener, IFXNewsPersister newsPersister) {
this.newsListener = newsListener;
this.newsPersister = newsPersister
}
public void getAndPersistNews() {
String[] newsIds = newsListener.getAvailableNewsIds();
if (ArrayUtils.isEmpty(newsIds)) {
return;
}
for(String newsId : newsIds) {
FXNewsBean newsBean = newsListener.getNewsByPK(newsId);
newsPersister.persistNews(newsBean);
newsListener.postProcessIfNecessary(newsId);
}
}
}
- 直接编码方式
public class BeanFactory {
public static void main(String[] args) {
// TODO Auto-generated method stub
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 RootBeanDefinition(FXNewsProvider.class, true);
AbstractBeanDefinition newsListener = new RootBeanDefinition(DowJonesNewsListener.class, true);
AbstractBeanDefinition newsPersister = new RootBeanDefinition(DowJonesNewsPersister.class, true);
// 将bean定义注册到容器中
registry.registerBeanDefinition("djNewsProvider", newsProvider);
registry.registerBeanDefinition("djListener", newsListener);
registry.registerBeanDefinition("djPersister", newsPersister);
// 指定依赖关系
// 1. 通过构造方法注入方式
ConstructorArgumentValues argValues = new ConstructorArgumentValues();
argValues.addIndexedArgumentValue(0, newsListener);
argValues.addIndexedArgumentValue(1, newsPersister);
newsProvider.setConstructorArgumentValues(argValues);
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.addPropertyValue(new propertyValue("newsListener", newsListener));
propertyValues.addPropertyValue(new propertyValue("newsPersister", newsPersister));
newsProvider.setPropertyValues(propertyValues);
return (BeanFactory)registry;
}
}
2. 外部配置文件方式
采用外部配置文件时,Spring的IoC容器有一个统一的处理方式。通常情况下,需要根据不同的外部配置文件格式,给出相应的BeanDefinitionReader实现类,由BeanDefinitionReader的相应实现类负责将相应的配置文件内容读取并映射到BeanDefinition。然后将映射后的BeanDefinition注册到一个BeanDefinitionRegistry,之后,BeanDefinitionRegistry即完成Bean的注册和加载。当然,大部分工作,包括解析文件格式、装配BeanDefinition之类的工作,都是由BeanDefinition-Reader的相应实现类来做的,BeanDefinitionRegistry只不过负责保管而已。
news-config.xml文件如下:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE bean PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="djNewsProvider" class="..FXNewsProvider"> <constructor-arg index = "0"> <ref bean="djNewsLisener"/> </constructor-arg> <constructor-arg index = "1"> <ref bean="djNewsPersister"/> </constructor-arg> </bean> <bean id="djNewsListener" class="..impl.DowJonesNewsListener"></bean> <bean id="djNewsPersister" class="..impl.DowJonesNewsPersister"></bean> </beans>
public static void main(String[] args) {
// TODO Auto-generated method stub
DefaultListableBeanFactory beanRegistry = new DefaultListableBeanFactory();
BeanFactory container = (BeanFactory)bindViaXMLFile(beanRegistry);
FXNewsProvider newsProvider = (FXNewsProvider)container.getBean("djNewsProvider");
newsProvider.getAndPersistNews();
}
public static BeanFacotry bindViaXMLFile(BeanDefinitionRegistry registry) {
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(registry);
reader.loadBeanDefinition("classpath:../news-config.xml");
return (BeanFactory)registry;
// 或者直接
// return new XmlBeanFactory(new ClassPathResource("../news-config.xml"));
}
3. 注解方式
@Component
public class FXNewsProvider {
@Autowired
private IFXNewsListener newsListener;
@Autowired
private IFXNewsPersister newsPersister;
public FXNewsProvider(IFXNewsListener newsListener, IFXNewsPersister newsPersister) {
this.newsListener = newsListener;
this.newsPersister = newsPersister
}
public void getAndPersistNews() {
String[] newsIds = newsListener.getAvailableNewsIds();
if (ArrayUtils.isEmpty(newsIds)) {
return;
}
for(String newsId : newsIds) {
FXNewsBean newsBean = newsListener.getNewsByPK(newsId);
newsPersister.persistNews(newsBean);
newsListener.postProcessIfNecessary(newsId);
}
}
}
@Autowired将告知Spring容器需要为当前对象注入哪些依赖对象。而@Component则是配合Spring2.5中心的classpath-scanning功能使用的。在配置文件中加入
<context:component-scan base-package="cn.spring21.project.base.package">
<context:component-scan/>会到指定的包下面扫描标注有@Component的类,如果找到,则将它们添加到容器进行管理,并根据它们所标准的@Autowired为这些类注入符合条件的依赖对象。在以上所有这些工作都完成之后,就可以像通常那样加载配置并执行当前应用程序了。