Announcement Announcement Module
Collapse
No announcement yet.
PropertyPlaceholderConfigurer and javconfig Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • PropertyPlaceholderConfigurer and javconfig

    How do I configure a PropertyPlaceholderConfigurer using the new javaConfig annotations.

    For example i have simple config class:
    Code:
    @Configuration
    public class FxFeed {
        private @Value("${jdbcProperties.url}") String jdbcUrl;
        private @Value("${jdbcProperties.username}") String username;
        private @Value("${jdbcProperties.password}") String password;
    
    
        @Bean
        public DataSource dataSource() throws SQLException {
            OracleDataSource ds = new OracleDataSource();
            ds.setURL(jdbcUrl);
            ds.setUser(username);
            ds.setPassword(password);
            return ds;
        }
    }
    How do i specify where to load the properties file from? I don't want to have a create a seperate xml file just to load the properties, i wanted to have a complete annotation based solution.

    Previous using xml config i would have:
    Code:
        <bean
    	class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    		<property name="locations">
    			<list>
    				<value>classpath:instance.properties</value>
    			</list>
    		</property>
    </bean>

  • #2
    Add this to your config file:

    Code:
    @Bean
    public PropertyPlaceholderConfigurer propertyConfigurer() throws IOException {
        PropertyPlaceholderConfigurer props = new PropertyPlaceholderConfigurer();
        props.setLocations(new Resource[] {new ClassPathResource("instance.properties")});
        return props;
    }
    This is the JavaConfig equivalent of your xml declaration.

    Comment


    • #3
      I tried adding the bean declaration like that but @Value annotations are not being processing with the appropriate value from the properties file as expected.

      Comment


      • #4
        Sorry my mistake...I took a look at the reference documentation, that is the way to load your file with java config:

        Code:
        @Configuration
        @ImportResource("classpath:properties-config.xml")
        public class FxFeed {
            private @Value("${jdbcProperties.url}") String jdbcUrl;
            private @Value("${jdbcProperties.username}") String username;
            private @Value("${jdbcProperties.password}") String password;
        
        
            @Bean
            public DataSource dataSource() throws SQLException {
                OracleDataSource ds = new OracleDataSource();
                ds.setURL(jdbcUrl);
                ds.setUser(username);
                ds.setPassword(password);
                return ds;
            }
        }
        And properties-config.xml has:

        Code:
        <beans>
        <context:property-placeholder location="classpath:instance.properties"/>
        </beans>
        Last edited by Enrico Pizzi; Apr 8th, 2011, 03:49 AM.

        Comment


        • #5
          Hi

          This was the point of my original question, is there a way to do this without creating the stub xml documentation just to load the properties? I am trying to configure my app using java config and i would prefer not to have to split my configuration into 2 places, having a xml for just the properties seems superfluous.

          thanks

          Comment


          • #6
            having a xml for just the properties seems superfluous
            I totally agree with you but, at least for now, that is the only way. From the reference documentation:

            In applications where @Configuration classes are the primary mechanism for configuring the container, it will still likely be necessary to use at least some XML. In these scenarios, simply use @ImportResource and define only as much XML as is needed. Doing so achieves a "Java-centric" approach to configuring the container and keeps XML to a bare minimum.
            It clearly states that some xml is unavoidable. Maybe you could open a Jira issue to make property reading 100% java-configurable. If it gets voted enough, the Spring guys will probably consider to implement it in the next Spring release. If you do so, be sure to specify the issue number on a post in this forum thread so that people interested will vote for it (me included).

            Comment


            • #7
              Originally posted by crafty78 View Post
              Hi
              This was the point of my original question, is there a way to do this without creating the stub xml documentation just to load the properties? I am trying to configure my app using java config and i would prefer not to have to split my configuration into 2 places, having a xml for just the properties seems superfluous.
              This is working for me with AnnotationConfigApplicationContext in 3.0.6.RELEASE:

              Code:
              @Configuration
              public class DbConfig {
              
              	@Bean
              	public PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() {
              
              		PropertyPlaceholderConfigurer propertyPlaceholderConfigurer = new PropertyPlaceholderConfigurer();
              		propertyPlaceholderConfigurer.setLocation(new ClassPathResource("db.properties"));
              
              		return propertyPlaceholderConfigurer;
              	}
              
              	@Bean
              	public DataSource dataSource(
              			@Value("${dbDriver}") String driverClass, 
              			@Value("${dbUrl}") String jdbcUrl,
              			@Value("${dbUsername}") String user, 
              			@Value("${dbPassword}") String password) throws PropertyVetoException {
              
              		ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
              
              		comboPooledDataSource.setDriverClass(driverClass);
              		comboPooledDataSource.setJdbcUrl(jdbcUrl);
              		comboPooledDataSource.setUser(user);
              		comboPooledDataSource.setPassword(password);
              		comboPooledDataSource.setCheckoutTimeout(CHECKOUT_TIMEOUT);
              
              		return comboPooledDataSource;
              	}
              }
              It does not work if I refactor the dataSource(...) parameters to fields as in your original example.

              The only related JIRA I could find is the umbrella SPR-7420 - Support Java-Based Application Configuration, which speaks of "Enabling property-placeholder replacement (<contextroperty-placeholder/>)". I've added my vote.

              Comment


              • #8
                Use @Import with a property class

                To use PropertyPlaceholderConfigurer with the Configuration annoation, but without xml, use the @Import annotation.

                Code:
                @Configuration
                public class DbConfigProperties {
                   
                    @Bean
                    public PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() {
                        PropertyPlaceholderConfigurer propertyPlaceholderConfigurer = new PropertyPlaceholderConfigurer();
                        propertyPlaceholderConfigurer.setLocation(new ClassPathResource("db.properties"));
                        // Allow for other PropertyPlaceholderConfigurer instances.
                        propertyPlaceholderConfigurer.setIgnoreUnresolvablePlaceholders(true);
                        return propertyPlaceholderConfigurer;
                    }
                
                }
                
                @Configuration
                @Import(DbConfigProperties.class)
                public class FxFeed {
                    private @Value("${jdbcProperties.url}") String jdbcUrl;
                    private @Value("${jdbcProperties.username}") String username;
                    private @Value("${jdbcProperties.password}") String password;
                
                
                    @Bean
                    public DataSource dataSource() throws SQLException {
                        OracleDataSource ds = new OracleDataSource();
                        ds.setURL(jdbcUrl);
                        ds.setUser(username);
                        ds.setPassword(password);
                        return ds;
                    }
                }
                This doesn't really do much except remove the need for the XML file, but that may be worthwhile in your project. In my case, the configuration, including the properties file, needs to be discovered dynamically.

                Much thanks to ibrandt for pointing me in the right direction.

                Comment

                Working...
                X