SpringBoot配置文件

(1) .yml和.yaml格式配置文件

application.yml

  lastName: zhangsan
  age: 18
  boss: false
  birth: 2019/10/10
  maps: {k1: v1,k2: v2}
  lists:
    - lisi
    - wangwu
    - zhaoliu
  myDog:
    name: 小狗
    age: 3

引用这个配置文件,这里出现@ConfigurationProperties注解报错,引入@Component解决

@Component
@ConfigurationProperties(prefix = "person")
public class Person {
    
    private  String lastName;
    private Integer age;
    private Boolean boss;
    private Date birth;
    private Map maps;
    private Listlists;
    private Dog myDog;
}

测试,在Test文件中

@SpringBootTest
@RunWith(SpringRunner.class)
class DemoApplicationTests {

    @Autowired
    private Person person;

    @Test
    void contextLoads() {
        System.out.println(person);
    }
}

这里需要注意,如果报错,请检查pom.xml文件是否配置正确,同时检查Person 实体及其他相关是否在根目录下,即与

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

在同一个目录下,正常配置,则可以看到,控制台会输出:
Person{lastName='zhangsan', age=18, boss=false, birth=Thu Oct 10 00:00:00 CST 2019, maps={k1=v1, k2=v2}, lists=[lisi, wangwu, zhaoliu], myDog=Dog{name='小狗', age=3}}
配置文件获取成功。

(2) .properties格式配置文件
person.last-name=zhangsan
person.age=12
person.birth=2015/15/15
person.boss=false
person.maps.k1=v1
person.maps.k2=v2
person.lists=a,b,c
person.my-dog.name=卡哇伊
person.my-dog.age=5

装配方式和上面一致,但是打印出来出现了中文乱码:
Person{lastName='lisi', age=12, boss=false, birth=Tue Mar 15 00:00:00 CST 2016, maps={k1=v1, k2=v2}, lists=[a, b, c], myDog=Dog{name='������', age=5}}
更改idea的Encoding可以解决。

(3) 加载指定的配置文件
@ConfigurationProperties 默认从全局文件中获取值
@ProperitySource加载指定的配置文件,实现与全局无关的配置单独设置配置文件

为上面例子中的Person单独写一个配置文件 personal.properties,内容与上面的application.properties一致的。
更改绑定方式为:

@PropertySource(value = {"classpath:personal.properties"})

也是可以在控制台看到输出结果的,但是有个问题如果把@ConfigurationProperties(prefix = "person")这个内容注释掉,输出的内容value都是null,不注释掉则正常输出绑定的personal.properties中的值,这点还没搞懂,但教程是这么干的。

(4) 导入Srping配置文件

Spring Boot没有Spring的配置文件,我们自己新建文件,也不能自动识别,想让Spring Boot 识别,我们需要借助@ImprotResouce来导入。
新建文件beans.xml



    

在主配置类上添加@ImportResource(locations = "classpath:beans.xml")

@ImportResource(locations = "classpath:beans.xml")
@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

添加测试

    @Autowired
    ApplicationContext ioc;

    @Test
    public void testHelloService() {
        boolean b = ioc.containsBean("helloService");
        System.out.println(b);
    }

输出为true,则spring的配置文件导入成功。

(5)SpringBoot推荐使的导入配置文件方法

用全注解的方式,不使用配置文件,使用配置类替代配置文件。

 * @Configuration 指明当前类是一个配置类,替代之前的sprig配置文件
 * 如何添加组件?
 * @Bean  将方法的返回值添加到容器中,这个组件默认的id就是当前方法名
 */
@Configuration
public class MyConfig {
    @Bean
    public HelloService helloService(){
        System.out.println("配置类给容器中添加组件");
        return new HelloService();
    }
}

测试

@RunWith(SpringRunner.class)
class HelloWorldTest {

    @Autowired
    private Person person;

    @Autowired
    ApplicationContext ioc;

    @Test
    public void testHelloService() {
        boolean b = ioc.containsBean("helloService");
        System.out.println(b);
    }

    @Test
    void main() {
        System.out.println(person);
    }}

控制台打印出

2019-11-07 09:43:35.990  INFO 9256 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-11-07 09:43:36.597  INFO 9256 --- [           main] c.l.s.HelloWorldTest                     : Started HelloWorldTest in 4.83 seconds (JVM running for 9.888)

true

证明配置文件导入成功。

(6)配置文件的占位符

1.随机数
person.last-name=张三${random.uuid}
person.age=1${random.int}
2.占位符获取之前配置的值,如果没有可以使用:指定默认值
person.my-dog.name=${person.hello:hello}_dog
如果person.hello获取不到值,则配置默认的“hello”这个值

(7)多环境支持,Profile
1.多Profile方式

我们在主配置文件编写的时候,可以写多个文件,文件名可以是application-{profile}.properties/yml
例如:
application-dev.properties
application-prod.properties
我们在这两个文件中,指定了不同的端口号,用于区分开发环境下使用的,和生产环境下使用的端口。

2.多文档块

yml有一种语法,可以分成很多的文档块。在同一个文件中就可以实现1和3中说的指定端口操作

  port: 8082
spring:
  profiles:
    active: dev
---
server:
  port: 8083
  spring:
    profiles: dev
---
server:
  port: 8084
  spring:
    profiles: prod
3.激活指定的profile

1.在配置文件中指定
spring.profiles.active=dev
这一局指定了使用开发环境的端口号,也就是我们第一步定义的那个。
2.命令行方式指定
3.虚拟机参数
-Dspring.profiles.active-dev

你可能感兴趣的:(SpringBoot配置文件)