Spring的XmlBeanFactory已经过时。

58
我试图学习Spring。我正在遵循这个网站:http://www.roseindia.net/spring/spring3/spring-3-hello-world.shtml 我尝试了其中的一个例子。我使用的代码类似于下面的代码,但是这里显示:

XmlBeanFactory类型已过时

请问我该使用什么替代方法?
public class SpringHelloWorldTest {
    public static void main(String[] args) {

        XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("SpringHelloWorld.xml"));

        Spring3HelloWorld myBean = (Spring3HelloWorld)beanFactory.getBean("Spring3HelloWorldBean");
        myBean.sayHello();
    }
}

-1 阅读javadoc。http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/beans/factory/xml/XmlBeanFactory.html - skaffman
12个回答

53

ApplicationContext是BeanFactory的子接口。您可以使用以下方式:

public class SpringHelloWorldTest {
    public static void main(String[] args) {
        ApplicationContext context= new ClassPathXmlApplicationContext("SpringHelloWorld.xml");
        Spring3HelloWorld myBean= (Spring3HelloWorld) context.getBean("Spring3HelloWorldBean");
        myBean.sayHello();
    }
}

我猜如果我提供完整的路径和文件名,它可能不起作用,而xmlBeanFactory将起作用,因为它的构造函数接受FileSystemResource对象。 - sar
我们也可以使用AbstractApplicationContext context = new ClassPathXmlApplicationContext("SpringHelloWorld.xml");代替ApplicationContext,因为我们可以使用close()来关闭容器。 - anandchaugule
1
通过使用AbstractApplicationContext类,我们还可以使用registerShudownHook()方法来确保优雅的关闭并调用可能与bean生命周期相关联的销毁方法。 - Revnic Robert-Nick

16

这是替代代码:

public static void main(String[] args){
    ApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"SpringHelloWorld.xml"});
    BeanFactory factory=context;
    Spring3HelloWorld myBean=(Spring3HelloWorld)factory.getBean("Spring3HelloWorldBean");
    myBean.sayHello();
}

嗨,谢谢,它起作用了。你能告诉我一些学习Spring的书籍和教程吗? - Vishwanath.M

11
BeanDefinitionRegistry beanDefinitionRegistry = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanDefinitionRegistry);
reader.loadBeanDefinitions(new ClassPathResource("SPRING_CONFIGURATION_FILE"));

7

获取Bean上下文的新方法(不需要进行类型转换):

BeanDefinitionRegistry beanFactory = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
reader.loadBeanDefinitions(new ClassPathResource("beans.xml"));

当启动应用程序范围的上下文时,应使用

ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");

6

嗨,谢谢,它起作用了。你能告诉我一些学习Spring的书籍和教程吗? - Vishwanath.M

2
这个怎么样:
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
reader.loadBeanDefinitions(new ClassPathResource("config/Beans.xml"));
Messager msg = (Messager) factory.getBean("Messager");

1
这是实现的最佳方式。
Resource res = new FileSystemResource("beans.xml");
XmlBeanFactory factory = new XmlBeanFactory(res);

or

ClassPathResource res = new ClassPathResource("beans.xml");
XmlBeanFactory factory = new XmlBeanFactory(res);

or

ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
       new String[] {"applicationContext.xml", "applicationContext-part2.xml"});
// of course, an ApplicationContext is just a BeanFactory
BeanFactory factory = (BeanFactory) appContext;

1

在Spring文档中找到的XMLBeanFactory的替代方案

GenericApplicationContext ctx = new GenericApplicationContext();
XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(ctx);
xmlReader.loadBeanDefinitions(new 
ClassPathResource("applicationContext.xml"));
PropertiesBeanDefinitionReader propReader = new 
PropertiesBeanDefinitionReader(ctx);
propReader.loadBeanDefinitions(new 
ClassPathResource("otherBeans.properties"));
ctx.refresh();

MyBean myBean = (MyBean) ctx.getBean("myBean");

0

0

使用 "FileSystemXmlApplicationContext"

ApplicationContext  context =  new FileSystemXmlApplicationContext("SpringHelloWorld.xml");

Spring3HelloWorld myBean= (Spring3HelloWorld) context.getBean("Spring3HelloWorldBean");

myBean.sayHello();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接