读取属性配置文件的五种方式
读取属性配置文件的五种方式
- @Value
- @ConfigurationProperties
- @PropertySource + @Value
- @PropertySource + ConfigurationProperties
- org.springframework.core.env.Environment
读取属性配置的示例
属性配置文件
application.properties
#服务端口号 server.port=9424 # redis配置 # Redis数据库索引(默认为0) spring.redis.database=0 # Redis服务器地址 spring.redis.host=127.0.0.1 # Redis服务器连接端口 spring.redis.port=6379 # Redis服务器连接密码(默认为空) spring.redis.password=
demo.properties
demo.name=huang demo.sex=1 demo.type=demo
方式一:使用注解@Value读取属性配置
package com.huang.pims.demo.props; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class ReadByValue { @Value("${server.port}") private int serverPort; @Override public String toString() { return "ReadByValue{" + "serverPort=" + serverPort + '}'; } }
使用此种方式,如无其他需求,可不写setter、getter方法。
方式二:使用注解@ConfigurationProperties读取属性配置
package com.huang.pims.demo.props; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "spring.redis") public class ReadByConfigurationProperties { private int database; private String host; private String password; private int port; public void setDatabase(int database) { this.database = database; } public void setHost(String host) { this.host = host; } public void setPassword(String password) { this.password = password; } public void setPort(int port) { this.port = port; } public int getDatabase() { return database; } public int getPort() { return port; } public String getHost() { return host; } public String getPassword() { return password; } @Override public String toString() { return "ReadByConfigurationProperties{" + "database=" + database + ", host='" + host + '\'' + ", password='" + password + '\'' + ", port=" + port + '}'; } }
使用此种方式,必须要有成员变量的setter、getter方法。
方式三:使用注解 @PropertySource 和 @Value 来读取属性配置
package com.huang.pims.demo.props; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; @Component @PropertySource(value = {"demo/props/demo.properties"}) public class ReadByPropertySourceAndValue { @Value("${demo.name}") private String name; @Value("${demo.sex}") private int sex; @Value("${demo.type}") private String type; @Override public String toString() { return "ReadByPropertySourceAndValue{" + "name='" + name + '\'' + ", sex=" + sex + ", type='" + type + '\'' + '}'; } }
注:使用@PropertySource注解读取属性配置,该种方式不支持读取yml配置文件
方式四:使用注解 @PropertySource 和 @ConfigurationProperties 来读取属性配置
package com.huang.pims.demo.props; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; @Component @PropertySource(value = {"demo/props/demo.properties"}) @ConfigurationProperties(prefix = "demo") public class ReadByPropertySourceAndConfProperties { private String name; private int sex; private String type; public void setName(String name) { this.name = name; } public void setSex(int sex) { this.sex = sex; } public void setType(String type) { this.type = type; } public String getName() { return name; } public int getSex() { return sex; } public String getType() { return type; } @Override public String toString() { return "ReadByPropertySourceAndConfProperties{" + "name='" + name + '\'' + ", sex=" + sex + ", type='" + type + '\'' + '}'; } }
方式五:使用环境变量 Environment 读取属性配置
package com.huang.pims.demo.props; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.env.Environment; import org.springframework.stereotype.Component; @Component public class ReadByEnv { @Autowired private Environment environment; public String getServerPort() { return environment.getProperty("server.port"); } }
测试类
package com.huang.pims.demo.runners; import com.huang.pims.demo.props.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; @Component public class OutputPropsRunner implements CommandLineRunner { private static final Logger LOGGER = LoggerFactory.getLogger(OutputPropsRunner.class); @Autowired private ReadByValue readByValue; @Autowired private ReadByConfigurationProperties readByConfigurationProperties; @Autowired private ReadByPropertySourceAndValue readByPropertySourceAndValue; @Autowired private ReadByPropertySourceAndConfProperties readByPropertySourceAndConfProperties; @Autowired private ReadByEnv readByEnv; @Override public void run(String... args) throws Exception { LOGGER.info(readByValue.toString()); LOGGER.info(readByConfigurationProperties.toString()); LOGGER.info(readByPropertySourceAndValue.toString()); LOGGER.info(readByPropertySourceAndConfProperties.toString()); LOGGER.info(readByEnv.getServerPort()); } }
测试方法,启动项目即可看到效果。
从截图中可以看出,需要读取的属性配置,都已经成功读取出来了。