-
Notifications
You must be signed in to change notification settings - Fork 22
Spring Utils Properties
Contains classes to work with properties and integratie with the Spring Environment abstraction.
This is an implementation of an ApplicationContextInitializer
which registers an additional PropertySource
with the Environment
. The properties in that PropertySource
are loaded from a database. It by default expects a table named Configuration
with 2 columns name
and value
. The default query that is being executed is select NAME, VALUE from CONFIGURATION
.
As this is an ApplicationContextInitializer
it has to be added to the web.xml
or WebApplicationInitializer
to have it registered with the context.
Note: The properties are loaded at startup, updates to the properties in the database aren't propagated as with using property files a restart of the application is required!
You can either use a context-param
or init-param
(for the DispatcherServlet
) to register the initializer. Create a param with the name contextInitializerClasses
.
For the ContextLoaderListener
.
<context-param>
<param-name>contextInitializerClasses</param-name>
<param-value>biz.deinum.core.env.JdbcPropertySourceInitializer</param-value>
</context-param>
For the DispatcherServlet
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<context-param>
<param-name>contextInitializerClasses</param-name>
<param-value>biz.deinum.core.env.JdbcPropertySourceInitializer</param-value>
</context-param>
</servlet>
or globally (both ContextLoaderListener
and DispatcherServlet
).
<context-param>
<param-name> globalInitializerClasses </param-name>
<param-value>biz.deinum.core.env.JdbcPropertySourceInitializer</param-value>
</context-param>
Assuming one uses the AbstractAnnotationConfigDispatcherServletInitializer
or one of the other classes in the hierarchy you can use the getServletApplicationContextInitializers
to register it for the DispatcherServlet
or getRootApplicationContextInitializers
to register it for the ContextLoaderListener
being used.
public class MyInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
protected ApplicationContextInitializer<?>[] getServletApplicationContextInitializers() {
return new ApplicationContextInitializer[] { new JdbcPropertySourceInitializer()}
}
protected ApplicationContextInitializer<?>[] getRootApplicationContextInitializers() {
return new ApplicationContextInitializer[] { new JdbcPropertySourceInitializer()}
}
}
After registration Spring will invoke the defined callback methods and the initialized PropertySource
will be added to the Environment
.
The JdbcPropertySourceInitializer
needs a few properties so that it knows which DataSource
to use. It can either use a DataSource
from JNDI or create one using a URL, username and password property.
The properties can be set as environment variables, system properties (-D
) or when using a web application the Servlet Context as well.
When the property config.jdbc.jndi-name
is set in the Environment
that will be used to do a lookup for the datasource.
When setting the config.jdbc.url
property that is used to construct a temporary DataSource
based on the DriverManagerDataSource
for Spring. Optionally a config.jdbc.username
and config.jdbc.password
property can also be specified.
By default the query that is being executed is select NAME, VALUE from CONFIGURATION
this can be overriden using the config.jdbc.query
property.
Note: When overriding the query make sure that it returns 2 columns, the first the name of the property the second the value of the property.