Injecting Properies in Spring boot
In this tutorial, we will find how to bind configuration properties to Structured Object.
Spring Boot allows you to externalize your configuration so you can work with the same application code in different environments. If these configurations are all related then we can bound these to java object using spring's @ConfigurationProperties annotation.
Spring Boot allows you to externalize your configuration so you can work with the same application code in different environments. If these configurations are all related then we can bound these to java object using spring's @ConfigurationProperties annotation.
Ways to make configuration object injected into application
- Annotate Application class with @EnableConfigurationProperties(value=MyProperties.class). In this case, there is no need to mark your configuration object with @Component annotation.
- Or mark your configuration object with @Component annotation. This will make your class to be picked up by Spring's component scanner.
Find this project on Github
https://github.com/javacodenet/spring-boot-injecting-propertiespom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>com.javacodenet</groupId> | |
<artifactId>spring-boot</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
<parent> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-parent</artifactId> | |
<version>1.4.0.RELEASE</version> | |
</parent> | |
<dependencies> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-web</artifactId> | |
</dependency> | |
</dependencies> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-maven-plugin</artifactId> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
Application.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javacodenet; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.beans.factory.annotation.Value; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
import org.springframework.boot.context.properties.EnableConfigurationProperties; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.RestController; | |
@RestController | |
@SpringBootApplication | |
//@EnableConfigurationProperties(value=MyProperties.class) This will be required if you remove @Component annotation | |
// from MyProperties.java | |
public class Application { | |
@Autowired | |
MyProperties myProperties; | |
@Autowired | |
CustomProperties customProperties; | |
@RequestMapping("/") | |
public String myMessageProperty() { | |
return "Value read from application.properties is: " + myProperties.getMessageProperty() + " " + | |
"And value read from custom.properties is: " + customProperties.getCustomMessageProperty(); | |
} | |
public static void main(String[] args) { | |
SpringApplication.run(Application.class, args); | |
} | |
} |
MyProperties.java
Reads properties from application.properties.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javacodenet; | |
import org.springframework.boot.context.properties.ConfigurationProperties; | |
import org.springframework.stereotype.Component; | |
@Component | |
@ConfigurationProperties(prefix = "my") | |
public class MyProperties { | |
private String messageProperty; | |
public String getMessageProperty() { | |
return messageProperty; | |
} | |
public void setMessageProperty(String messageProperty) { | |
this.messageProperty = messageProperty; | |
} | |
} |
CustomProperties.java
Reads properties from custom.properties. We specified the location of the file as classpath along with its name.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javacodenet; | |
import org.springframework.boot.context.properties.ConfigurationProperties; | |
import org.springframework.stereotype.Component; | |
@Component | |
@ConfigurationProperties(prefix = "my", locations = "classpath:custom.properties") | |
public class CustomProperties { | |
private String customMessageProperty; | |
public String getCustomMessageProperty() { | |
return customMessageProperty; | |
} | |
public void setCustomMessageProperty(String customMessageProperty) { | |
this.customMessageProperty = customMessageProperty; | |
} | |
} |
application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#my is the prefix and MESSAGE_PROPERTY is the final node | |
my.MESSAGE_PROPERTY="Message Property" | |
#Following are the different ways of defining your properties | |
#my.messageProperty="Message Property" | |
#my.message-property="Message Property" |
custom.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
my.CUSTOM_MESSAGE_PROPERTY="Custom Message Property" |
Comments
Post a Comment