1. 使用 @Value 读取配置文件
使用@Value读取配置文件,这种方法适用于对象的参数比较少的情况,我们可以直接在对象属性上使用@Value注解,同时以${}的形式传入配置文件对应的属性,同时需要在该类的方法上使用@Configuratin注解,将该类作为配置
- 我们在 Spring Boot 配置文件 application.properties 添加以下内容:
profile.name=编程开发
profile.desc=一站式博客,响应式编程
- 业务代码层面的实现
@SpringBootApplication
public class DemoApplication implements InitializingBean {
@Value("${profile.name}")
private String name;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("我的项目名:" + name);
}
}
2. 使用 @ConfigurationProperties 读取配置文件
@ConfigurationProperties 和 @Value 的使用略微不同,@Value 是读取单个配置项的,而 @ConfigurationProperties 是读取一组配置项的,我们可以使用 @ConfigurationProperties 加实体类读取一组配置项
当对象参数比较多的情况下,推荐使用@ConfigurationProperties会简单一些,不需要每个字段的上面都加上@Value了
- 在使用@ConfigurationProperties的时候注解生命在当前类为配置读取类,prefix=“rabbitmq”,表示读取前缀为rabbitmq的属性
- 示例在实体类上的代码如下:
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "profile")
@Data
public class Profile {
private String name;
private String desc;
}
- 业务代码使用,其中 prefix 表示读取一组配置项的根 name,相当于 Java 中的类名,最后再把此配置类,注入到某一个类中就可以使用了,如下代码所示:
@SpringBootApplication
public class DemoApplication implements InitializingBean {
@Autowired
private Profile profile;
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token class-name">SpringApplication</span><span class="token punctuation">.</span><span class="token function">run</span><span class="token punctuation">(</span><span class="token class-name">DemoApplication</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">,</span> args<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token annotation punctuation">@Override</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">afterPropertiesSet</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span> <span class="token punctuation">{</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"我的项目名:"</span> <span class="token operator">+</span> profile<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
}
3. 使用 Environment 的注入方式读取配置文件
Environment 是 Spring Core 中的一个用于读取配置文件的类,将此类使用 @Autowired 注入到类中就可以使用它的 getProperty 方法来获取某个配置项的值
- 这个是用也是比较简单,直接在业务类中注入即可
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.env.Environment;
@SpringBootApplication
public class DemoApplication implements InitializingBean {
@Autowired
private Environment environment;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("我的项目名:" + environment.getProperty("profile.name"));
}
}
4. 使用 @PropertySource 读取配置文件
@ Propertysource注解用于指定资源文件读取的位置,它不仅能读取 properties文件,也能读取xml文件,并且通过YAML解析器,配合自定义 Propertysourcefactory实现解析YAML文件
- 这个感觉不如@Value使用方便,以下是我的测试案例
@SpringBootApplication
@PropertySource("classpath:application.properties")
public class DemoApplication implements InitializingBean {
@Value("${profile.name}")
private String name;
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token class-name">SpringApplication</span><span class="token punctuation">.</span><span class="token function">run</span><span class="token punctuation">(</span><span class="token class-name">DemoApplication</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">,</span> args<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token annotation punctuation">@Override</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">afterPropertiesSet</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span> <span class="token punctuation">{</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"我的项目名:"</span> <span class="token operator">+</span> name<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
}
4.1. 中文乱码问题
如果配置文件中出现中文乱码的情况,可通过指定编码格式的方式来解决中文乱码的问题,具体实现如下:
@PropertySource(value = "application.properties", encoding = "utf-8")
5. 总结
在 Spring Boot 中读取配置文件有以下 4 种方法:
- 使用 @Value 读取配置文件。
- 使用 @ConfigurationProperties 读取配置文件。
- 使用 @PropertySource 读取配置文件。
- 使用 Environment 读取配置文件。