Springs XmlBeanFactory is deprecated

蹲街弑〆低调 提交于 2019-11-26 19:44:31

问题


I try to learn Spring. I am following this site http://www.roseindia.net/spring/spring3/spring-3-hello-world.shtml

I tried one example in that. I am using some what like below, but here it shows:

The type XmlBeanFactory is deprecated

What do I have to use as an alternative to this?

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:


ApplicationContext is a sub-interface of BeanFactory.You can use this way

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



回答2:


Here is the substitute code,

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



回答3:


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



回答4:


You can use the ClassPathXmlApplicationContext class.




回答5:


New way to get beans context (without class casting):

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

When starting an apppication-wide context one should use

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



回答6:


Here is the best way to implement

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;



回答7:


how about this:

DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
reader.loadBeanDefinitions(new ClassPathResource("config/Beans.xml"));
Messager msg = (Messager) factory.getBean("Messager");



回答8:


Alternative to XMLBeanFactory found on Spring documentation

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");



回答9:


Use "FileSystemXmlApplicationContext" as

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

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

myBean.sayHello();



回答10:


There is a warning "Resource leak: 'context' is never closed" with the accepted answer.

The solution suggested in the SO post Spring ApplicationContext - Resource leak: 'context' is never closed fixes the issue.

Hope it helps someone.




回答11:


I tried the following code

    public class Spring3HelloWorldTest {
    public static void main(String[] args) {        
        DefaultListableBeanFactory  beanFactory = new DefaultListableBeanFactory ((BeanFactory) new ClassPathResource("SpringHelloWorld.xml"));     
        Spring3HelloWorld myBean = (Spring3HelloWorld) beanFactory.getBean("Spring3HelloWorldBean");
        myBean.sayHello();
    }
}

and it works



来源:https://stackoverflow.com/questions/5231371/springs-xmlbeanfactory-is-deprecated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!