Skip to content

Instantly share code, notes, and snippets.

@danveloper
Last active December 17, 2015 14:19
Show Gist options
  • Save danveloper/5623263 to your computer and use it in GitHub Desktop.
Save danveloper/5623263 to your computer and use it in GitHub Desktop.
Groovy Configuration DSL for environment properties in Spring
package com.danveloper.reactor.config
reactor {
social {
twitter {
consumerKey = "ABCD"
consumerSecret = "EFGH"
accessToken = "IJKL"
accessTokenSecret = "MNOP"
}
}
}
@Configuration
class SpringConfiguration {
@Bean
public static PropertyPlaceholderConfigurer props() {
def config = new ConfigSlurper().parse(ApplicationConfiguration)
def configurer = new PropertyPlaceholderConfigurer()
configurer.setProperties(config.toProperties())
configurer
}
@Bean
public TemplateFactory templateFactory() {
new TemplateFactory()
}
}
@Service
class TemplateFactory {
@Value('${reactor.social.twitter.consumerKey}')
private String consumerKey
@Value('${reactor.social.twitter.consumerSecret}')
private String consumerSecret
@Value('${reactor.social.twitter.accessToken}')
private String accessToken
@Value('${reactor.social.twitter.accessTokenSecret}')
private String accessTokenSecret
TwitterTemplate createTwitterTemplate() {
new TwitterTemplate(consumerKey, consumerSecret, accessToken, accessTokenSecret)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment