Skip to content

Instantly share code, notes, and snippets.

@inancsevinc
Created August 2, 2012 06:55
Show Gist options
  • Save inancsevinc/3234630 to your computer and use it in GitHub Desktop.
Save inancsevinc/3234630 to your computer and use it in GitHub Desktop.
Spring ApplicationContextInitializer class which adds a new PropertySource to Spring's DefaultEnvironment.
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.core.env.PropertySource;
import org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup;
import org.springframework.web.context.ConfigurableWebApplicationContext;
/**
* Adds a new PropertySource(settings table in db) to Spring's
* DefaultEnvironment before application context is refreshed. This class should
* be registered as a servlet context param(contextInitializerClasses) in
* web.xml
*
* @see <a
* href="http://blog.springsource.org/2011/02/15/spring-3-1-m1-unified-property-management/">
SPRINGSOURCE BLOG: UNIFIED PROPERTY MANAGEMENT</a>
*
* @author Inanc Sevinc
*
*/
public class PropertySourceInitializer implements ApplicationContextInitializer<ConfigurableWebApplicationContext> {
public void initialize(ConfigurableWebApplicationContext applicationContext) {
applicationContext.getEnvironment().getPropertySources()
.addLast(new PortalSettingsPropertySource("portalsettings"));
}
private interface SettingsMapper {
@Select("select PROPERTY_VALUE from PORTAL_SETTINGS where PROPERTY_KEY = #{key}")
String selectProperty(String key);
}
private static class PortalSettingsPropertySource extends
PropertySource<String> {
private static final String JNDI_DATASOURCE_NAME = "jdbc/verim";
private static final Map<String,String> settingsMap = new HashMap<String,String>();
public PortalSettingsPropertySource(String name) {
super(name);
}
@Override
public String getProperty(String key) {
String value = settingsMap.get(key);
if(value!=null)
return value;
synchronized (settingsMap) {
value = getPropValueFromDB(key);
settingsMap.put(key, value);
};
return value;
}
private String getPropValueFromDB(String key) {
SqlSession sqlSession = null;
try {
// programmatically create mybatis sessionfactory
Configuration configuration = new Configuration(
new Environment("verim",
new JdbcTransactionFactory(),
new JndiDataSourceLookup().getDataSource(JNDI_DATASOURCE_NAME)
));
// add mapper interface before executing query
configuration.addMapper(SettingsMapper.class);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
.build(configuration);
sqlSession = sqlSessionFactory.openSession();
SettingsMapper mapper = sqlSession.getMapper(SettingsMapper.class);
String result = mapper.selectProperty(key);
if (result != null) {
System.out.println("INFO: Configuration value is retrieved from DB. key="+key+" value="+result);
return result;
} else {
System.out.println("ERROR: Configuration value could not be found in DB. key="+key);
}
} catch (Exception e) {
System.err.print("Exception in "+this.getClass().getCanonicalName()+".getPropValueFromDB()\n"+ExceptionUtils.getStackTrace(e));
} finally {
if (sqlSession != null)
sqlSession.close();
}
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment