0

I'm trying to create a custom property placeholder.

At my Spring Configuration Class I have this:

@Configuration
@Import(ConfigurationClass2.class)
public class ConfigurationClass1 {
    @Bean
    @Lazy(false)
    public static PropertySourcesPlaceholderConfigurer settings() throws IOException {
         // custom load of my properties file
         // legacy configuration, comes from a SVN repository
         // I need to download it and then do some logic and finally 
         // load it.
    }
}

At another configuration class I want to access some properties using the Environment, like this:

@Configuration
@EnableScheduling
public class ConfigurationClass2 {

    @Autowired
    private Environment env;

    @Bean
    public DataSource dataSource() {
        String jdbcUrl = env.getProperty('jdbc.url');
        .... // jdbcUrl is null!!!!
    }

    @Scheduled(cron = "${cronExpression}")
    public void worker() {
    } 

}

I don't want to use @Value or @PropertySource.

I need to load this properties manually and access this values programmatically!

How can I solve this?

Beto Neto
  • 3,962
  • 7
  • 47
  • 81
  • 1
    What is wrong with using `@PropertySource` that is basically the same as loading it yourself, also it is mandatory if you want to use the `Environment` to access your properties. If you don't they are only known to your `PropertySourcesPlaceholderConfigurer` and nothing else. – M. Deinum Nov 26 '14 at 13:43
  • M. Deinum take a look at the changes of the post... this will explain why I cannot use @PropertySource – Beto Neto Nov 26 '14 at 13:49
  • Create an `ApplicationContextInitializer` which does the loading of the properties and adds `PropertySource` to the `Environment`. That way they are accesible through the `Environment` and the `PropertySourcesPlaceholderConfigurer` still works as well. – M. Deinum Nov 26 '14 at 13:51
  • M. Deinum, how can I use this ApplicationContextInitializer when I'm not using web.xml but using the AbstractAnnotationConfigDispatcherServletInitializer ? – Beto Neto Nov 26 '14 at 14:01
  • You can override the `customizeRegistration` and add an init parameter with the name `contextInitializerClasses` to specify the `ApplicationContextInitializer` to use. – M. Deinum Nov 26 '14 at 14:38
  • M. Deinum, this did not worked. The ApplicationContextInitializer is called after Configuration classes. I need to read the properties before, to use them in the Configuration class via Environment – Beto Neto Nov 26 '14 at 15:42
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/65701/discussion-between-beto-neto-and-m-deinum). – Beto Neto Nov 26 '14 at 15:52
  • Then you did something wrong as they are really read before that. I made the assumption that the classes were read by the `DispatcherServlet` but they are probably read by the `ContextLoaderListener`. If that is the case then override the `onStartup` method register the same init parameter there and call `super.onStartup` afterwards. You can then of course remove the the overridden `customizeRegistration` method. – M. Deinum Nov 26 '14 at 18:37

0 Answers0