{"id":23172,"date":"2015-07-23T00:27:37","date_gmt":"2015-07-22T18:57:37","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=23172"},"modified":"2016-08-26T17:29:19","modified_gmt":"2016-08-26T11:59:19","slug":"binding-external-configuration-properties-in-spring-boot-application","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/binding-external-configuration-properties-in-spring-boot-application\/","title":{"rendered":"Binding External Configuration properties in spring boot application"},"content":{"rendered":"<p>Hi,<\/p>\n<p>In this blog\u00a0we&#8217;ll learn how we can inject configuration properties in a springBoot application and what are the benefits of spring boot over spring while injecting configuration properties.<\/p>\n<p><strong>There are two ways to inject our configuration properties<\/strong>:-<\/p>\n<p>1. By Using @Value annotation (<strong>Spring 3.0+ And Grails 3.0<\/strong>)<br \/>\n2. Typesafe Configuration Properties (<strong>Sring-Boot<\/strong>)<\/p>\n<p>Lets suppoese we have a yaml(application.yml) as configuration file, i.e., the following YAML document:<\/p>\n<pre>es:\r\n  hosts: ['5.9.154.115']\r\n  port: 10600\r\n  cluster : 'elasticsearch'\r\n<\/pre>\n<p>We can also use the properties file(application.properties) for external configuration, the properties file version for the above yml file would be:-<\/p>\n<pre>es.hosts[0]='5.9.154.115'\r\nes.port=10600\r\nes.cluster='elasticsearch'\r\n<\/pre>\n<p>1. Using <b>@Value<\/b> annotation we can inject the configuration properties into our application. Syntax would be <b>@Value(#{property_name}<\/b>).<\/p>\n<p>For example, the following class represent binding the external configuration in a class using the @value annotation :-<\/p>\n<p>[code]<br \/>\n@Configuration<br \/>\npublic class SpringConfig {<br \/>\n    static Client esClient<\/p>\n<p>    @Value(&quot;#{es.hosts}&quot;)<br \/>\n    List hosts<\/p>\n<p>    @Value(&quot;#{es.port}&quot;)<br \/>\n    int port<\/p>\n<p>    @Value(&quot;#{es.cluster}&quot;)<br \/>\n    String cluster<\/p>\n<p>    @Bean<br \/>\n    Client getClient() {<br \/>\n        if (!esClient) {<br \/>\n            Settings settings = ImmutableSettings.settingsBuilder().put(&quot;cluster.name&quot;, cluster).build();<br \/>\n            TransportClient transportClient = new TransportClient(settings);<br \/>\n            for (String esHostName : hosts) {<br \/>\n                transportClient = transportClient.addTransportAddress(new InetSocketTransportAddress(esHostName, port));<br \/>\n            }<br \/>\n            esClient = transportClient<\/p>\n<p>        }<\/p>\n<p>        return esClient<br \/>\n    }<br \/>\n}<\/p>\n<p>[\/code]<\/p>\n<p>The &#8216;springConfig&#8217; class properties i.e. hosts , port and cluster will automatically bind with their corresponding match with external configuration file, hosts will bind to es.hosts, port bind to es.port and so on.<\/p>\n<p>We can also define the default value in @Value annotation using <b>Elvis operator(?:)<\/b>. Syntax for the same is: #{expression?:default value}.<\/p>\n<p>For Example :<br \/>\n<code>@Value(\"#{es.port?:9200}\")<\/code><\/p>\n<p>Note: Using the @Value(&#8220;#{property_name}&#8221;) annotation to inject configuration properties can sometimes be confusing, especially if we are working with multiple properties.<\/p>\n<p>2. Typesafe Configuration Properties:- Spring Boot provides an alternative method of working with configuration properties that allows strongly typed beans to inject and validate the configuration properties of our application.<\/p>\n<p>For example, the following class represent binding the external configuration in a class using <b>@ConfigurationProperties<\/b> annotation :-<\/p>\n<p>[code]<br \/>\n@Configuration<br \/>\n@EnableConfigurationProperties<br \/>\n@ConfigurationProperties(prefix = &quot;es&quot;)<br \/>\nclass SpringConfig {<br \/>\n    static Client esClient<\/p>\n<p>    List hosts<br \/>\n    int port<br \/>\n    String cluster<\/p>\n<p>    @Bean<br \/>\n    Client getClient() {<br \/>\n        if (!esClient) {<br \/>\n            Settings settings = ImmutableSettings.settingsBuilder().put(&quot;cluster.name&quot;, cluster).build();<br \/>\n            TransportClient transportClient = new  TransportClient(settings);<br \/>\n            for (String esHostName : hosts) {<br \/>\n                transportClient = transportClient.addTransportAddress(new InetSocketTransportAddress(esHostName, port));<br \/>\n            }<br \/>\n            esClient = transportClient<\/p>\n<p>        }<\/p>\n<p>        return esClient<br \/>\n    }<br \/>\n}<br \/>\n[\/code]<\/p>\n<p>In @ConfigurationProperties annotation we can define property&#8217;s prefix. In our case any property defined with the &#8216;es&#8217; prefix will be mapped onto that &#8216;SpringConfig&#8217; bean, hosts will bind to es.hosts, port bind to es.port and so on.<\/p>\n<p>To validate the external configuaration we can use JSR-303(javax.validate) constraints annotations to our @ConfiqurationPropertis class<\/p>\n<p>For Example:-<\/p>\n<p>[code]<br \/>\n@Configuration<br \/>\n@EnableConfigurationProperties<br \/>\n@ConfigurationProperties(prefix = &quot;es&quot;)<br \/>\nclass SpringConfig {<br \/>\n    static Client esClient<\/p>\n<p>    @NotNull<br \/>\n    List hosts<\/p>\n<p>    @NotNull<br \/>\n    int port<\/p>\n<p>    @NotNull<br \/>\n    String cluster<\/p>\n<p>    @Bean<br \/>\n    Client getClient() {<br \/>\n        if (!esClient) {<br \/>\n            Settings settings = ImmutableSettings.settingsBuilder().put(&quot;cluster.name&quot;, cluster).build();<br \/>\n            TransportClient transportClient = new  TransportClient(settings);<br \/>\n            for (String esHostName : hosts) {<br \/>\n                transportClient = transportClient.addTransportAddress(new InetSocketTransportAddress(esHostName, port));<br \/>\n            }<br \/>\n            esClient = transportClient<\/p>\n<p>        }<\/p>\n<p>        return esClient<br \/>\n    }<br \/>\n}<br \/>\n[\/code]<\/p>\n<p>The above @ConfiqurationProperties class will validate against null values. And we can also add a custom Spring Validator by creating a bean definition called <b>ConfigurationPropertiesValidator<\/b>.<\/p>\n<p>Hope this helps you \ud83d\ude42<\/p>\n<p>Get in touch with our team to discuss <a title=\"spring consulting\" href=\"http:\/\/www.tothenew.com\/java-development-services\">your project on Spring<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi, In this blog\u00a0we&#8217;ll learn how we can inject configuration properties in a springBoot application and what are the benefits of spring boot over spring while injecting configuration properties. There are two ways to inject our configuration properties:- 1. By Using @Value annotation (Spring 3.0+ And Grails 3.0) 2. Typesafe Configuration Properties (Sring-Boot) Lets suppoese [&hellip;]<\/p>\n","protected":false},"author":92,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":24},"categories":[1],"tags":[2009,2012,207,1202,2011,2010],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/23172"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/92"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=23172"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/23172\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=23172"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=23172"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=23172"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}