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