Posted on 2011-11-28 22:24
沙漠中的鱼 阅读(766)
评论(0) 编辑 收藏 所属分类:
开源框架 、
Java
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;
}
}