目录
1、通过@value注入
2、通过@ConfigurationProperties注入
3、通过框架自带对象Environment实现属性动态注入
4、通过@PropertySource注解实现外部配置文件注入属性值
5、yml 外部配置文件动态注入
6、Java原生态方式注入属性值
满足条件:
1、该类首先要被SpringBoot框架管理,属于SpringBoot框架的Bean。
2、application.properties(或者application.yml)配置文件中存在该配置名。(如果不存在可以加冒号,框架会赋值默认值,也可以在冒号后面自定义默认值。例如:test.domain:)。配置文件中不包含该配置,注解中也没加冒号,项目启动就会报错。
3、被static和finely修饰的属性配置该注解不会生效。
@Component
public class BaseConfig {
@Value("${test.domain:}")
private String domamin;
@Value("${test.api:}")
private String api;
}
1、该类首先要被SpringBoot框架管理,属于SpringBoot框架的Bean。
2、@ConfigurationProperties进行指定配置文件中key的前缀。进行自动装配属性。适用于批量绑定,批量注入属性值。相比@value更省事。
#application.yml配置文件
es: post:9200 host:localhost name:es
@Data
@Component
@ConfigurationProperties(prefix="es")
public class ESProperties {
private String host;
private String name;
private int port;
}
#application.yml配置文件
es: post:9200 host:localhost name:es
方式一:容器自动注入SpringBoot框架自带的类Environment进行实现动态配置属性值注入。
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component
public class EsProperties {
private String host;
private String name;
private int post;
@Resource
private Environment environment;
public String getHost() {
return environment.getProperty("es.host");
}
public String getName() {
return environment.getProperty("es.name");
}
public int getPost() {
String post = environment.getProperty("es.post");
return Integer.parseInt(post);
}
}
方式二:自己实现EnvironmentAware接口进行重写方法进行注入Environment。好处可以和spring boot框架的解耦性更低一点。
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
@Component
public class EsProperties implements EnvironmentAware {
private String host;
private String name;
private int post;
private Environment environment;
public String getHost() {
return environment.getProperty("es.host");
}
public String getName() {
return environment.getProperty("es.name");
}
public int getPost() {
String post = environment.getProperty("es.post");
return Integer.parseInt(post);
}
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
}
}
#es.properties配置文件 es.post=9200 es.host=localhost es.name=es
1、通过@PropertySource注解实现导入外部配置文件。
2、配合@value注解实现属性注入或者@ConfigurationProperties注解实现批量注入。
3、该方式只能获取 .properties 的配置文件不能获取 .yml 的配置文件。
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@PropertySource(value = "classpath:es.properties",encoding = "utf-8")
public class EsProperties {
@Value("${es.host}")
private String host;
@Value("${es.name}")
private String name;
@Value("${es.post}")
private int post;
}
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@PropertySource(value = "classpath:es.properties",encoding = "utf-8")
@ConfigurationProperties(prefix = "es")
public class EsProperties {
private String host;
private String name;
private int post;
}
第4中方式只能实现 .properties 文件的,该方式是实现 .yml 文件的。
1、自定义配置类,实例化PropertySourcesPlaceholderConfigurer类,使用该类进行属性值的注入。
2、实例化完PropertySourcesPlaceholderConfigurer类之后,就可以配合@value注解实现属性注入或者@ConfigurationProperties注解实现批量注入。
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;
import java.util.Objects;
@Configuration
public class MyYmlConfig {
@Bean
public static PropertySourcesPlaceholderConfigurer yamlConfigurer(){
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
yaml.setResources(new ClassPathResource("es.yml"));
configurer.setProperties(Objects.requireNonNull(yaml.getObject()));
return configurer;
}
}
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class EsProperties {
@Value("${es.host}")
private String host;
@Value("${es.name}")
private String name;
@Value("${es.post}")
private int post;
}
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "es")
public class EsProperties {
private String host;
private String name;
private int post;
}
#es.properties配置文件
es.post=9200es.host=localhost
es.name=es
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Properties;
@Component
public class EsProperties {
private String host;
private String name;
private int post;
@Bean
public void init(){
Properties props = new Properties();
InputStreamReader inputStreamReader = new InputStreamReader(
this.getClass().getClassLoader().getResourceAsStream("es.properties"), StandardCharsets.UTF_8);
try {
props.load(inputStreamReader);
} catch (IOException e) {
System.out.println(e.getMessage());
}
this.host = props.getProperty("es.host");
this.name = props.getProperty("es.name");
this.post = Integer.parseInt(props.getProperty("es.post"));
}
}