-
-
Save jeffsheets/8ab5f3aeb74787bdb051 to your computer and use it in GitHub Desktop.
| /** | |
| * Example of Spring 4 Properties Java Configuration, | |
| * with a Database Properties table to store most values | |
| * and a small application.properties file too. | |
| * The Database table will take precedence over the properties file with this setup | |
| */ | |
| @Configuration | |
| @PropertySource(value = { "classpath:application.properties" }, ignoreResourceNotFound=true) | |
| public class SpringPropertiesConfig { | |
| private static final Logger log = LoggerFactory.getLogger(SpringPropertiesConfig.class); | |
| @Inject | |
| private org.springframework.core.env.Environment env; | |
| @PostConstruct | |
| public void initializeDatabasePropertySourceUsage() { | |
| MutablePropertySources propertySources = ((ConfigurableEnvironment) env).getPropertySources(); | |
| try { | |
| //dataSource, Table Name, Key Column, Value Column | |
| DatabaseConfiguration databaseConfiguration = new DatabaseConfiguration(dataSource(), "AppConfiguration", "propertyKey", "propertyValue"); | |
| //CommonsConfigurationFactoryBean comes from https://java.net/projects/springmodules/sources/svn/content/tags/release-0_8/projects/commons/src/java/org/springmodules/commons/configuration/CommonsConfigurationFactoryBean.java?rev=2110 | |
| //Per https://jira.spring.io/browse/SPR-10213 I chose to just copy the raw source into our project | |
| CommonsConfigurationFactoryBean commonsConfigurationFactoryBean = new CommonsConfigurationFactoryBean(databaseConfiguration); | |
| Properties dbProps = (Properties) commonsConfigurationFactoryBean.getObject(); | |
| PropertiesPropertySource dbPropertySource = new PropertiesPropertySource("dbPropertySource", dbProps); | |
| //By being First, Database Properties take precedence over all other properties that have the same key name | |
| //You could put this last, or just in front of the application.properties if you wanted to... | |
| propertySources.addFirst(dbPropertySource); | |
| } catch (Exception e) { | |
| log.error("Error during database properties setup", e); | |
| throw new RuntimeException(e); | |
| } | |
| } | |
| /** | |
| * Read why this is required: http://www.baeldung.com/2012/02/06/properties-with-spring/#java | |
| * It is important to be static: http://www.java-allandsundry.com/2013/07/spring-bean-and-propertyplaceholderconf.html | |
| */ | |
| @Bean | |
| public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { | |
| return new PropertySourcesPlaceholderConfigurer(); | |
| } | |
| @Bean | |
| public DataSource dataSource() | |
| { | |
| BasicDataSource dataSource = new BasicDataSource(); | |
| dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName")); | |
| dataSource.setUrl(env.getProperty("jdbc.url")); | |
| dataSource.setUsername(env.getProperty("jdbc.username")); | |
| dataSource.setPassword(env.getProperty("jdbc.password")); | |
| return dataSource; | |
| } | |
| } |
@ag-projects, it sounds like you are loading multiple values for a single variable if I understand correctly. As such, I'd recommend having two columns in the database for key/value and then the value could be a comma-separated list of values.
sorry for the slow response, github doesn't notify of gist comments. ping me @Sheetsj on twitter if you're still stuck
@jeffsheets: Does this work when we extract data from Mongo DB as well? If not, what would you suggest to user for NoSQL (Mongo) DB?
Will it work when we have @value
@muhdkhokhar it's been a few years, but yes, i'm 95% sure we were using @value annotations with this config
hmm its very intersting do you have full implemenation somewhere its not clear.
If you have working copy with altest spring it would be amainzg
@muhdkhokhar sorry, i don't have the full code anymore from ~8 years ago... But I'm pretty sure it would still work. A co-worker adapted this pattern to read from AWS a few years ago https://objectpartners.com/2020/09/24/setup-spring-datasource-from-values-stored-in-aws-secret-manager/
Hi Jeff, i have a question for you.
I am trying to pull data from a column in a table from a sql-db. This is a list of strings, which i need to be loaded in a bean in my application, so i can use it. Currently i have a file saved in the file system, which i get the list and inject it as a resource in my beans and use this string list. How can i accomplish this similar to what you are doing in this example?
Thank you,
Armen