springboot-Three simple ways to read properties of spring boot configuration files
1. The purpose of this post
I would demo three simple ways to read properties of spring boot configuration files..
2. Environments
- springboot 1.x and 2.x
- java 1.8+
3. Way 1: Read from application.properties by @Value annotation
Add these properties to your default configuration file: src/main/resources/application.properties
info.name=Mike
info.desc=Good boy
Use @Value like this:
package com.test.sb1jt.commands;
import com.test.sb1jt.config.InfoConfig;
import com.test.sb1jt.config.SysConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
public class MyCommand implements CommandLineRunner {
private static Logger logger = LoggerFactory.getLogger(MyCommand.class);
@Value("${info.name}") //1.Auto configure name property by reading info.name from application.properties
private String name;
@Value("${info.desc}") //2.Auto configure desc property by reading info.desc from application.properties
private String desc;
@Override
public void run(String... strings) throws Exception {
logger.info("MyCommand run");
logger.info("name is {},desc is {}",name,desc); //3. print the name and desc
}
}
Run the code ,we get this:
2019-05-23 12:45:58.827 INFO 31595 --- [ main] com.test.sb1jt.commands.MyCommand : name is Mike,desc is Good boy
4. Way 2: Read from application.properties by @ConfigurationProperties annotation
Instead of @Value annotion, you can use @ConfigurationProperties,which can read some specific prefixed properties from your application.properties to your class properties.
Add these properties to your springboot application.properties
info.name=Mike
info.desc=Good boy
Add a class which use @ConfigurationProperties like this:
package com.test.sb1jt.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "info")
public class InfoConfig {
private String name; // info.name mapped to this property
private String desc; // info.desc mapped to this property
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}
Then print properties like this:
package com.test.sb1jt.commands;
import com.test.sb1jt.config.InfoConfig;
import com.test.sb1jt.config.SysConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import com.test.sb1jt.config.InfoConfig;
@Component
public class MyCommand implements CommandLineRunner {
private static Logger logger = LoggerFactory.getLogger(MyCommand.class);
@Autowired
private InfoConfig infoConfig;
@Override
public void run(String... strings) throws Exception {
logger.info("MyCommand run");
logger.info("infoconfig name is {},desc is {}",infoConfig.getName(),
infoConfig.getDesc());
}
}
Run the code ,we get this:
2019-05-23 12:45:58.827 INFO 31595 --- [ main] com.test.sb1jt.commands.MyCommand : infoconfig name is Mike,desc is Good boy
5. Way 3: Read from your custom xxx.properties by @ProperySource annotation
By reading previous sections, you can see that by using @Value or @ConfigurationProperties, it’s easy to read properties in springboot’s default application.properties, but what should we do if we want read properties from a custom properties file?
According to this document,Spring provides @PropertySource annotation to read custom file properties.
Create a new file named sysconfig.properties in src/main/resources/ , add these properties to it:
sysname = BDS
sysdesc = Big data system
Create a new class to use the @PropertySource like this:
package com.test.sb1jt.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@PropertySource(value={"sysconfig.properties"}) //read from your custom src/main/resources/sysconfig.properties
public class SysConfig {
@Value("${sysname}") // mapping from sysname to this property
private String sysName;
@Value("${sysdesc}") // mapping from sysdesc to this property
private String sysDesc;
public String getSysName() {
return sysname;
}
public void setSysName(String sysName) {
this.sysName = sysName;
}
public String getSysDesc() {
return sysDesc;
}
public void setSysDesc(String sysDesc) {
this.sysDesc = sysDesc;
}
}
Print the custom properties like this:
package com.test.sb1jt.commands;
import com.test.sb1jt.config.InfoConfig;
import com.test.sb1jt.config.SysConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
/**
* Created on 2019/5/22.
*/
@Component
public class MyCommand implements CommandLineRunner {
private static Logger logger = LoggerFactory.getLogger(MyCommand.class);
@Autowired
private SysConfig sysConfig; //inject the sysConfig instance
@Override
public void run(String... strings) throws Exception {
logger.info("MyCommand run");
logger.info("sysconfig sysname is {},sysdesc is {}",
sysConfig.getSysName(),sysConfig.getSysDesc());
}
}
Run the code ,we get this:
2019-05-23 12:45:58.827 INFO 31595 --- [ main] com.test.sb1jt.commands.MyCommand : sysconfig sysname is BDS,sysdesc is Big data system
4. Conclusion
You can use @Value ,@Configuration to read default application.properties or use the @PropertySource in your custom properties file in SpringBoot apps.