Announcement Announcement Module
Collapse
No announcement yet.
@Bean PropertiesFactoryBean not loading properly Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @Bean PropertiesFactoryBean not loading properly

    Hello,

    I am trying to switch my application from XML config to Java config and I'm running into some problems with Properties

    On a side note, I need to have a separate PropertiesFactoryBean, since I reuse them in a controller, but everything else uses the PlaceHolderConfiguration


    This is my original configuration, and everything works perfectly :
    Code:
        <bean id="applicationProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
            <property name="ignoreResourceNotFound" value="true" />
            <property name="locations">
                <list>
                    <value>file:etc/application.properties</value>
                </list>
            </property>
        </bean>
    
        <bean id="applicationConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="properties" ref="applicationProperties" />
            <property name="ignoreUnresolvablePlaceholders" value="false" />
        </bean>

    And then I tried to switch it to Java config like this :
    Code:
        @Bean(name="applicationProperties")
        public static PropertiesFactoryBean applicationPropertiesFactoryBean() {
            PropertiesFactoryBean ppc = new PropertiesFactoryBean();
            ppc.setLocations(new Resource[] {
                                        new FileSystemResource("etc/application.properties")
                                    });
            
            ppc.setIgnoreResourceNotFound(true);
            return ppc;
        }
        
        public static Properties applicationProperties() throws Exception {
            return applicationPropertiesFactoryBean().getObject();
        }
        
        @Bean
        public static PropertySourcesPlaceholderConfigurer properties() {
            PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
            ppc.setProperties(applicationProperties());
            
            ppc.setIgnoreResourceNotFound(true);
            ppc.setIgnoreUnresolvablePlaceholders(false);
            return ppc;
        }

    Yet every single time the properties are accessed, it always returns null
    - I've tried setting up the FactoryBean not static
    - I've tried removing it from the PlaceholderConfig and using it only in my Controller
    - I've tried many little changes left and right, and it's still null when I call it

    Am I missing something?
    Or is there a problem with PropertiesFactoryBean?


    I ended up setting the applicationProperties in XML, and using the setLocations for my PlaceHolder, and everything works fine, but I'd like to bring everything in Java with the annotations



    Thanks

  • #2
    I suggest a read of the reference guide about the new PropertySource support. You could then use the Environment to retrieve the properties or next to the PropertySource support also configure the PropertiesFactoryBean just to be used by the controllers.

    Comment


    • #3
      I forgot to mention, I tried that too, didn't fix the problem on my Controller, since it still receives a null from the PropertiesFactoryBean

      Every other property in my application works perfectly finee, I have no problems whatsoever with the placeholder no matter what method I use, it's only with PropertiesFactoryBean that things get ugly

      Comment


      • #4
        The PropertiesFactoryBean shouldn't be static it should be an normal method. I also assume you are on Spring 3.1 and not 3.0?

        Comment


        • #5
          Wow, I just realized that for the entire day yesterday, after I removed the static to test this, I also messed up my config at the same time, so that all my tests were invalid :P

          I retried to give you the result I got yesterday and everything worked perfectly ... Code 18 FTW
          Also for reference : I'm on Spring 3.2

          Thanks a lot for your help

          Comment

          Working...
          X