Announcement Announcement Module
No announcement yet.
reloading bean properties at runtime Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • reloading bean properties at runtime

    Hi everybody,

    I am want to know how i can change/load bean properties at runtime in my web application.
    I have a web application. I have a class which first get the applicationContext like this.

    ClassPathXmlApplicationContext appContext =new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"});

    messPro = (MessageProvider) appContext.getBean("provider");

    System.out.println(" Message:"+messPro.getMessage());

    I am calling the above code through JSP.

    "provider" bean is diffined like this in applicationContext.xml

    <bean id="provider" class="">
    <value>This is a configurable message</value>

    if i change the configurable message and call my class through JSP i give's me the old message. even though i am creating new applicationContext everytime i hit the class. i also tried using BeanFactory.retest() and close() but no luck.

    Any help in this regards would be greatly appreciated.

    thank you,

  • #2
    If you are creating a new ApplicationContext, refreshing the beans, etc.... then unless you are physically updating the XML with your new value it won't be there. Can't you simply set the value on your provider object?


    • #3
      no. I meant when i change the message inside ApplicationContext.xml and then call getMessage() on my bean it return me the old message not new message. So i want to know what should i do to read the new messages without restarting the web container.


      • #4
        Ok so you want to do two things; #1 write the value out to the XML and #2 set the value on the bean. #2 should be straight forward, #1 search the forum there has been discussions about updating the XML before.


        • #5
          I am able to do it using XmlBeanFactory(new FileSystemResource("applicationContext.xml")

          and now if i change the bean property value in the applicationContext.xml and call new on XmlBeanFactory i get a new property value.

          But the only problem i have is application looking for applicationContext.xml inside tomcat bin directory. how i can make it to read from WEB-INF directory. I tried XmlBeanFactory(new FileSystemResource("/WEB-INF/applicationContext.xml") but doesn't work.


          • #6
            You could use ClassPathResource instead. If you are loading another ApplicationContext however, you are going to be loading all the contained beans another time.


            • #7
              yeah i tried ClassPathResource and it worked but the problem is if i change the bean property value in ApplicationContext.xml then i doesn't load the new bean difination everytime i call

              new XmlBeanFactory(new ClassPathResource("applicationContext.xml")

              this is really strange?


              • #8
                I'm not really sure you want to be reloading the ApplicationContext anyway. You are going to be creating all the contained beans again. I would have thought it might be better to simply update the currently loaded bean and change the applicationContext.xml. That way next time the context has to be loaded it contains the correct value.


                • #9
                  No we are looking for to change the bean property values in applicationContext while the application is running and application should pick up those new values without restart.


                  • #10
                    Originally posted by muhwas View Post
                    No we are looking for to change the bean property values in applicationContext while the application is running and application should pick up those new values without restart.
                    That's exactly what I just suggested. You don't want to restart, agreed! So update the applicationContext.xml, if the server is restarted it will have the correct value. As well as this, physically call the setter on the bean with your new value. That should solve both your problems.


                    • #11
                      Assuming you are using SpringMVC also.

                      public class MessageController extends SimpleFormController {
                          private ConfigurableMessageProvider provider
                      	protected Object formBackingObject(HttpServletRequest request) throws Exception {
                      		MessageOBject formBackingObject = new MessageObject();
                      		return formBackingObject;
                      	protected ModelAndView onSubmit(Object command) throws Exception {
                      		MessageObject cmdObject = (MessageObject) command;
                      		return null; // or a mav of your choice.
                      public class MessageObject {
                      	private String message;
                      	public void setMessage(String message) {
                      	public String getMessage() {
                      		return this.message;
                      Your jsp contains then something like

                      	COnfigurable message: <form:input path="message">
                      	<input type="Submit">
                      And you should be good to go.

                      Just on a note this wont persist the change to the xml file, this still has to be done manually. However the messages changes without a restart, change it manually in your applicationContext.xml and it will chagne the next time the server is restarted. Or find another way to store that value i.e. ina database.


                      • #12
                        I was able to solve this problem using following code

                        try { resBundle = ResourceBundle.getBundle("MyProject"); }
                        catch (Exception ex) { ex.printStackTrace(); }

                        appConfigFile = resBundle.getString("Application.Context.File");
                        ac = new FileSystemXmlApplicationContext(appConfigFile);

                        In the resource bundle i define the full path to the application context. and then load the application context using FileSystemXmlApplicationContext. Now when ever i change any bean property i get the latest value without restarting the application. I don't know somehow ClassPathXmlApplicationContext doesn't seems to be reload/refresh bean property values if you call

                        new ClassPathXmlApplicationContext(appConfigFile);