泥瓦匠
🔥🔥 点击领取 ChatGPT Plus 正版账号

Spring Boot 配置文件详解

🔥🔥 点击领取 ChatGPT Plus 正版账号

一.自动配置

Spring Boot提供了对应用进行自动化配置,相比以前XML配置的方式,很多显式的申明是不需要的。

二者,大多数默认的配置足够实现开发功能,从而更快速的开发。

什么是自动配置?

Spring Boot 提供了默认的配置,如默认的Bean, 去运行Spring应用,他是非侵入的,只提供 一个默认的实现。

二.自定义属性

闲话不多说,我们可以开始看代码,编写其CityProperties的java对象


import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "city")
public class CityProperties {

    private Long id;

    private String cityName;

    private String desc;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getCityName() {
        return cityName;
    }

    public void setCityName(String cityName) {
        this.cityName = cityName;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    @Override
    public String toString() {
        return "CityProperties{" +
                "id=" + id +
                ", cityName='" + cityName + '\'' +
                ", desc='" + desc + '\'' +
                '}';
    }
}

通过@ConfigurationProperties(prefix=”city”)注解,将配置文件中以city前缀属性值自动绑定到

对应的字段中,同时用@Component作为Bean注入到Spring容器中

其中CityProperties对应的配置文件(aplication.properties)

#city的配置
city.id = 1
city.cityName = wlmq
city.desc = ${city.cityName} is most beautiful city

#user的配置
user.id = 2
user.userName = sunyanzi
user.desc = dev:${user.userName} is my god girl

如果不是用application.properties文件,而是用application.yml文件,则对应配置如下:

city:
id: 1
cityName: 克拉玛依
desc: ${city.cityName} is beautiful city!

键值对冒号必须空一格

项目首先会读取application.properties,如果没有的情况下,才会读取application.yml

注意这里,这个里面有个坑:

application.properties配置中文值的时候,读取出来的属性值会出现乱码。但是application.yml不会

出现乱码问题。原因Spring Boot是以iso-8859的编码方式来读取application.properties文件。

注意这里,还有一个坑:

如果定义一个键值对user.name=xxx,这里会读取不到对应的属性值,为什么呢?Spring Boot的默认会

加载System.getProperty(“user.name”),windows中机器会读当前登录的用户

三.多环境配置

很多场景的配置,比如数据库配置,Redis配置,注册中心和日志配置等。在不同环境下,我们需要不同的包去

运行项目。所以看项目结构,有两个环境的配置

application-dev.properties:正式环境
application-test.properties:测试环境

Spring Boot是通过application.properties文件中,设置spring.properties属性,比如,配置了dev,则加载的是

application-dev.properties以及其属性的输出。

CityProperties{id=1, cityName='wlmq', desc='wlmq is most beautiful city'}

Spring Boot是通过application.properties文件中,设置spring.properties属性,比如,配置了test,则加载的是

application-test.properties以及其属性的输出。

CityProperties{id=1, cityName='北京', desc='北京 最好看的城市'}

四.Spring Boot的配置优先级

Spring Boot不单单从application.properties获取配置,我们可以在程序中多种设置配置属性,按照以下列表优先级排列:

  • 1.命令行参数
  • 2.java:comp/env 里的 JNDI 属性
  • 3.JVM 系统属性
  • 4.操作系统环境变量
  • 5.RandomValuePropertySource 属性类生成的 random.* 属性
  • 6.应用以外的 application.properties(或 yml)文件
  • 7.打包在应用内的 application.properties(或 yml)文件
  • 8.在应用 @Configuration 配置类中,用 @PropertySource 注解声明的属性文件
  • 9.SpringApplication.setDefaultProperties 声明的默认属性

五.所需注解

@Component , @Service , @Repository , @Controller将自动注册为Spring Beans。将被自动注册为Spring Beans

  • @Service用于标注业务层组件
  • @Controller用于标注控制层组件(如struts中的action)
  • @Repository用于标注数据访问组件,即DAO组件
  • @Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注

依赖包的引入:pom.xml中仅依赖spring-boot-boot-starter-test,它吧相关的依赖全部引入

@RunWith(SpringRunner.class)告诉JUnit运行使用Spring的测试支持,Springrunner是SpringJUnit4ClassRunner的新名字,这个名字只是让名字看其简单些

@SpringBootTest的意思是”带有Spring Boot支持的引导程序”(例如,加载应用程序,属性,为我们提供Spring Boot的所有精华部分)

🔥🔥 点击领取 ChatGPT Plus 正版账号
QRCode

本文由 泥瓦匠 创作

原创不易,欢迎关注公众号!转载请注明出处,感谢支持!如果本文对您有用,欢迎转发分享!





本作品采用 知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议 (CC BY-NC-ND 4.0) 进行许可。