0

Currently, my application loads a properties file which has different properties separated by commas as

property=propertyA,propertyB,propertyC

I have a code which loads properties file and assigns as application properties as:

public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    private String[] properties;

    private PropertySource propertySource;

    public WebAppInitializer() {
            propertySource = getPropertySource();
            String propertyStr = (String) propertySource.getProperty("property");
            List<String> list = Lists.newArrayList(Splitter.on(',').trimResults().omitEmptyStrings().split(propertyStr));
            properties = list.toArray(new String[list.size()]);
        }
    }

    //    @Override
    public void onStartup(ServletContext ctx) throws ServletException {
        super.onStartup(ctx);

    }
protected WebApplicationContext createRootAppContext() {
    Class[] configClasses = this.getRootConfigClasses();
    if (!ObjectUtils.isEmpty(configClasses)) {
        AnnotationConfigWebApplicationContext rootAppContext = new AnnotationConfigWebApplicationContext();
        MyPropertySource.addToEnvironment(rootAppContext.getEnvironment());
        rootAppContext.getEnvironment().getPropertySources().addFirst(propertySource);
        rootAppContext.getEnvironment().setActiveProfiles(properties);
        rootAppContext.register(configClasses);
        return rootAppContext;
    } else {
        return null;
    }
}

protected WebApplicationContext createServletAppContext() {
    AnnotationConfigWebApplicationContext servletAppContext = new AnnotationConfigWebApplicationContext();
    servletAppContext.getEnvironment().setActiveProfiles(properties);
    Class[] configClasses = this.getServletConfigClasses();
    if (!ObjectUtils.isEmpty(configClasses)) {
        servletAppContext.register(configClasses);
    }
    return servletAppContext;
}
}

Now, we are migrating these properties to Db and want to load from DB. Initially, I thought, of adding below code:

        @Autowired
        PropertyRepository propertyRepository;
    public WebAppInitializer() {
                List<String> listDB = propertyRepository.getProperties();
                if(listDB.isEmpty()) {
    propertySource = getPropertySource();
                String propertyStr = (String) propertySource.getProperty("property");
                List<String> list = Lists.newArrayList(Splitter.on(',').trimResults().omitEmptyStrings().split(propertyStr));
                properties = list.toArray(new String[list.size()]);
            }
}
//Other pieces of code
}

But, the PropertyRepository is always coming as null. I guess Spring has not yet initialized the application. Did I miss anything here? Or is there an other way I should be following? I have to load the properties at this stages as I have different features initialized based on these properties.

user123475
  • 1,065
  • 4
  • 17
  • 29
  • Is the whole call trace through your application autowired? Or do you instantiate some classes manually? – Sven Hakvoort Oct 08 '18 at 13:14
  • it's via Autowired – user123475 Oct 08 '18 at 13:40
  • OK, you want to use DB properties for Spring Context initialization. Of course, you have no Spring IoC available before that context is created. There is still lots of code missing, e.g. the magic `MyPropertySource` class, and the point where your `WebAppInitializer` is instantiated / referenced to (an XML file, perhaps?) – Florian Albrecht Oct 09 '18 at 07:18

0 Answers0