在 Spring Boot 应用中,我们通常需要一些配置信息来指导应用的运行。这些配置信息可以包括如下内容:端口号、数据库连接信息、日志配置、缓存配置、认证配置、等等。Spring Boot 提供了多种方式来读取这些配置信息。读取配置的目的是为了在程序中使用这些配置信息,以便对应用进行正确的配置和处理。读取配置的目的是为了方便我们管理和配置应用,让应用能够按照自己的要求进行工作。
@Value
注解
@Value("${myapp.name}")
private String appName;
配置内容
结果
如果没用配置对应的内容,会报错,需要填写默认值
@Value("${myapp.name:defaultValue}")
private String appName;
结果
@ConfigurationProperties
注解
可以批量注入,比value注解方便
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;@ConfigurationProperties(prefix = "myapp")
@Configuration
@Data
public class MyAppProperties {private String name;private Integer age;}
测试
@Resourceprivate MyAppProperties myAppProperties;@Testpublic void test22(){System.out.println(myAppProperties.getName());System.out.println(myAppProperties.getAge());}
结果
Environment
对象(实现接口)
import lombok.Data;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;import javax.annotation.Resource;
@Configuration
@Data
public class Environmentconfig implements EnvironmentAware {private Environment env;@Overridepublic void setEnvironment(org.springframework.core.env.Environment environment) {this.env= environment;}
}
测试
@Resourceprivate Environmentconfig environmentconfig;@Testpublic void test22(){String property = environmentconfig.getEnv().getProperty("myapp.name");System.out.println(property);}
结果
Environment
对象(注入方式)
import lombok.Data;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;import javax.annotation.Resource;
@Configuration
@Data
public class Environmentconfig {@Resourceprivate Environment env;}
测试
@Resourceprivate Environmentconfig environmentconfig;@Testpublic void test22(){String property = environmentconfig.getEnv().getProperty("myapp.name");System.out.println(property);}
结果
@PropertySource
注解
可以读取外部配置
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.core.env.Environment;import javax.annotation.Resource;@Configuration
@PropertySources({@PropertySource(value = "classpath:myapp.properties",encoding = "utf-8")
})
@Data
public class AppConfig {@Value("${test.name}")private String name;
}
写一个外部配置文件
测试
@Resourceprivate AppConfig appConfig;@Testpublic void test22(){System.out.println(appConfig.getName());}
PropertySourcesPlaceholderConfigurer
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.ClassPathResource;@Configuration
public class MyYamlConfig {@Beanpublic static PropertySourcesPlaceholderConfigurer yamlConfigurer(){PropertySourcesPlaceholderConfigurer configurer= new PropertySourcesPlaceholderConfigurer();YamlPropertiesFactoryBean yaml= new YamlPropertiesFactoryBean();yaml.setResources(new ClassPathResource("myapp.yml"));configurer.setProperties(yaml.getObject());return configurer;}}
外部yml配置文件
测试
@Value("${test.name}")private String name;@Testpublic void test22(){System.out.println(name);}
结果
java原生
@Testpublic void test22(){Properties props=new Properties();try {InputStreamReader inputStreamReader=new InputStreamReader(this.getClass().getClassLoader().getResourceAsStream("myapp.properties"),StandardCharsets.UTF_8);props.load(inputStreamReader);} catch (Exception e) {throw new RuntimeException(e);}System.out.println(props.getProperty("test.name"));}
或者读取yml文件
@Testpublic void test22(){YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();factory.setResources(new ClassPathResource("myapp.yml"));Properties properties = factory.getObject();System.out.println(properties.getProperty("test.name"));}
配置文件
测试