• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

dynamic loading of bean values in spring?

 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<bean id="mybean" class="com.example.Example">
<property name="hour" value="10" />
</bean>

I have given above bean definition in spring xml file and get its instance in a following way:

ctx.getBean("mybean");

Now I want to change the attribute "hours" value from 10 to 11 in the spring context file so that next time when I get the bean instance the value of hour field is 11 not 10.

currently inspite of changing it is returning 10 since the context file in which bean is defined has already been loaded and to get the instance with updated value I have to restart the entire application.

Is there any way so that I get the instance with the updated value?

 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In general, there wouldn't be a need to reload the configuration after ApplicationContext startup. However, there is a refresh() method you can call on an ApplicationContext, I don't recall the exact interface that method is defined, but there is one.

You see the line

ApplicationContext context = new ClassPathXmlApplicationContext("config-file.xml");

reads in the configuration and immediately creates Java objects to represent the xml, once that is done the xml is no longer read or used. So you would have to tell Spring to re-read it.

You can always use Spring JMX and expose that property as an MBean attribute that you can go through a JMX console like jconsole, which comes with the JDK, go to that mbean at runtime and change the value. This will not change the xml, but will change the bean in the Spring container to now have that new value at runtime.

Mark
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is also now the Spring Expression language in Spring 3.x that you can use for dynamic values.

Mark
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic