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(); } }
- XMLBeanFactory is a bean factory that is loaded its beans from an XML file.
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.
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.
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.
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(); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With