Results 1 to 5 of 5

Thread: @Bean PropertiesFactoryBean not loading properly

  1. #1
    Join Date
    Jun 2012
    Posts
    6

    Default @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. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    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.
    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
    Join Date
    Jun 2012
    Posts
    6

    Default

    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

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

    Default

    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?
    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
    Join Date
    Jun 2012
    Posts
    6

    Default

    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

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
  •