adeelmahmood
adeelmahmood

Reputation: 2431

adding property sources in configuration classes

I have to load a properties file and the path of which is determined at runtime along with figuring out the properties file format. Which means I cant just specify the location. I have a properties loader class that reads the file and converts into key value pairs. In the past I would do this by implementing ApplicationContextInitializer but in this application I am using configuration classes and there is no web.xml file to register this listener in. How can I tap into the startup process of a @EnableWebMvc configuration class to call my properties loader class and add the map returned from it as a proeprty source. Any help would be appreciated.

Upvotes: 1

Views: 615

Answers (1)

Jonathan W
Jonathan W

Reputation: 3799

To hook an ApplicationContextListener into a Servlet 3.0 environment, you can do this one of two ways:

  • include a web.xml or web-fragment.xml which defines the contextInitializerClasses context-param whose value is the class name for the implementation. You can have both annotations and either / both of these deployment descriptors in your web app (the XML files are of higher precendence than annotated classes)
  • call setInitParameter("contextInitializerClasses", MyImplementingInitializer.class.getName()) method on the ServletContext class from a class that implements WebApplicationInitializer, annotating the class with @Order(1) to ensure that it runs before other WebApplicationInitializers

Upvotes: 1

Related Questions