2

I created a FactoryBean<Properties> as

public final class SystemProperteisFactoryBean implements FactoryBean<Properties> {

    private static final String QUERY = "select * from tb_system_properties";

    private final NamedParameterJdbcTemplate jdbcTemplate;

    public SystemProperteisFactoryBean (DataSource datasource) {
        this.jdbcTemplate = new NamedParameterJdbcTemplate (datasource);
    }

    @Override
    public Properties getObject() {
        Properties result = new Properties();
        jdbcTemplate.query(QUERY,
            (ResultSet rs) -> result.setProperty(rs.getString(1), rs.getString(2));
        return result;
    }

    @Override
    public Class<?> getObjectType() {
        return Properties.class;
    }

    @Override
    public boolean isSingletone() {
        return true;
    }
}

This class worked fine using Spring with XML config, I get DataSource using a JNDI name, and then created proper properties, and then used propertiesPlaceHoldeConfigurer via XML tag.

Now I want to use the same thing in Spring Boot and Java Config.

When I define a ProprtySourcesPlaceHolderConfigurer as a bean (in a static method in a @Configuration class) Spring tries to create this bean before the datasource.

Is there any way to create the datasource before PRopertySourcesPlaceHolderConfigurer?

2
  • can you use @PostConstruct to instantiate it? Commented Feb 13, 2018 at 11:22
  • can you show how you create datasource Commented Feb 13, 2018 at 11:37

2 Answers 2

0

Basically you need to take the dependency as a @Bean method parameter this way:

@Configuration
public static class AppConfig {
    @Bean
    public SystemPropertiesFactoryBean systemProperties(DataSource dataSource) {
      return new SystemPropertiesFactoryBean(dataSource);
    }

    @Bean
    public DataSource dataSource() {
      //  return new data source
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Could I use the default datasource created by Spring Boot? (the one configured in application.properties)?
sure, spring will try to find bean by type DataSource here and construct it first, before SystemPropertiesFactoryBean
I wanted to use Spring Boot's default DataSource (which is configurable to be JNDI based or created using a connection-pool provider). But it seems it is not possible using Java Config. Because Spring Boot tries to configure a ProprtySourcesPlaceHolderConfigurer before configuring most of the beans including the DataSource.
0

I have the similar type of requirement where the properties should get loaded from database and binded to the respective spring beans.

I written the example in the below github repository.

Load Properties from DB

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.