Results 1 to 7 of 7

Thread: PropertyOverrideConfigurer does not work

  1. #1

    Question PropertyOverrideConfigurer does not work

    Hello,

    I have a problem to define the connection properties for my data source.

    I have defined the properties for the data source in the spring context which are used in the default case.
    But at runtime I get other parameters for the data source. So I created an object of PropertyOverrideConfigurer in my java code to assign the parameters to that object. After that I refresh my application context. Here is the code snippet:

    PropertyOverrideConfigurer propertyOverrideConfigurer = new PropertyOverrideConfigurer();
    propertyOverrideConfigurer.setProperties( myProperties );
    propertyOverrideConfigurer.postProcessBeanFactory( applicationContext.getBeanFactory() );
    applicationContext.refresh();

    But nothing happens. The data source is still using the default properties which are defined in the context.xml.
    Did I forget something?

    Kind regards
    Sascha

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,629

    Default

    The ApplicationContext is going to refresh itself which means destroy everything and reload, so what you are doing here is well pretty much useless. You need to register the PropertyOverrideConfigurer with the BeanFactory/ApplicationContext not externally execute the postProcessBeanFactory because that isn't going to work/help.

    on refresh the 'refreshBeanFactory' method is called which first destroys the beans and then closes the BeanFactory, which basically means undoing what you have done.

    P.S. Use [ code][/code ] tags when posting code.

    Code:
    PropertyOverrideConfigurer propertyOverrideConfigurer = new PropertyOverrideConfigurer();
    propertyOverrideConfigurer.setProperties( myProperties );
    applicationContext.addBeanFactoryPostProcessor(propertyOverrideConfigurer);
    applicationContext.refresh();
    I assume here that 'applicationContext' is a ConfigurableApplicationContext.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3

    Default

    Yes, my applicationcontext is an instance of ClassPathXmlApplicationContext.

    I removed the calling of applicationContext.refresh(), but it still does not work. I have to defined the PropertyOverrideConfigurer externally because I have to set the Properties to the PropertyOverrideConfigurer at runtime.

    Maybe there is another possibility to overwrite the properties at runtime?

    Kind regards
    Sascha

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,629

    Default

    Sigh... Have you actually READ my post...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5

    Default

    Hmm ... I think so. Maybe I read your solution over. Can you give me a more detailed description of your solution?

    Kind regards,
    Sascha

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,629

    Default

    Compare the code below (my code)
    Code:
    PropertyOverrideConfigurer propertyOverrideConfigurer = new PropertyOverrideConfigurer();
    propertyOverrideConfigurer.setProperties( myProperties );
    applicationContext.addBeanFactoryPostProcessor(propertyOverrideConfigurer);
    applicationContext.refresh();
    with your code

    Code:
    PropertyOverrideConfigurer propertyOverrideConfigurer = new PropertyOverrideConfigurer();
    propertyOverrideConfigurer.setProperties( myProperties );
    propertyOverrideConfigurer.postProcessBeanFactory( applicationContext.getBeanFactory() );
    applicationContext.refresh();
    It is just a single line but an important difference... (hence my suggestion that you re-read my post).

    I no where said that you cannot create the bean externally, you must only REGISTER the bean else everything you do is useless....
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  7. #7

    Smile

    I am sorry that I read your solution over.

    Now it works. I am deeply indebted to you. Thank you.

    Kind regards,
    Sascha

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •