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