Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Springs XmlBeanFactory is deprecated

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();     } } 
like image 714
Vishwanath.M Avatar asked Mar 08 '11 10:03

Vishwanath.M


People also ask

What is XMLBeanFactory?

- XMLBeanFactory is a bean factory that is loaded its beans from an XML file.

What is the difference between ApplicationContext and BeanFactory in spring framework?

The ApplicationContext comes with advanced features, including several that are geared towards enterprise applications, while the BeanFactory comes with only basic features. Therefore, it's generally recommended to use the ApplicationContext, and we should use BeanFactory only when memory consumption is critical.

What is Defaultlistablebeanfactory?

Default implementation of the ListableBeanFactory and BeanDefinitionRegistry interfaces: a full-fledged bean factory based on bean definition objects. Typical usage is registering all bean definitions first (possibly read from a bean definition file), before accessing beans.

What is spring ApplicationContext?

The ApplicationContext InterfaceThe Spring IoC container is responsible for managing the objects of an application. It uses dependency injection to achieve inversion of control. The interfaces BeanFactory and ApplicationContext represent the Spring IoC container.


2 Answers

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();     } } 
like image 87
halilibrahim Avatar answered Sep 20 '22 14:09

halilibrahim


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(); } 
like image 34
krishna Avatar answered Sep 21 '22 14:09

krishna