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