Springs XmlBeanFactory is deprecated

左心房为你撑大大i 提交于 2019-11-27 18:56:52
halilibrahim

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

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();
}
BeanDefinitionRegistry beanDefinitionRegistry = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanDefinitionRegistry);
reader.loadBeanDefinitions(new ClassPathResource("SPRING_CONFIGURATION_FILE"));

You can use the ClassPathXmlApplicationContext class.

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

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;

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

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

Use "FileSystemXmlApplicationContext" as

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

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

myBean.sayHello();

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.

Rosanero4Ever

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

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