Avoid Spring ApllicationContext instanciation

Posted by Ceddoc on Stack Overflow See other posts from Stack Overflow or by Ceddoc
Published on 2013-08-12T13:42:37Z Indexed on 2013/11/02 21:54 UTC
Read the original article Hit count: 248

Filed under:
|
|

I use Spring 3 to make a simple configuration.

I have an XML file called PropertyBeans.xml like that :

<bean id="propertyBean" class="com.myapp.PropertyBean">
    <property name="rootDirLogPath" value="C:\Users\dede" />
</bean>

I have the bean which match this XML and then I want to use this bean with the value injected. Actually I have :

ApplicationContext context = new ClassPathXmlApplicationContext("AppPropertyBeans.xml");
PropertyBean obj = (PropertyBean) context.getBean("propertyBean");
String rootDirLogPath = obj.getRootDirLogPath();

This works great but I want to know if there's a way to avoid the instantiation of ApplicationContext at each time I want to use a bean. I've heard about BeanFactory is that a good idea? Which are the others solutions?

In other words: Am I supposed to called this Application context instanciation in every Controller in spring MVC?

© Stack Overflow or respective owner

Related posts about java

Related posts about spring