initialize properties after loading from database while starting web app Spring

230 Views Asked by At

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.

0

There are 0 best solutions below