springboot 加载配置文件的方式

1.Environment 可以通过Environment的getProperty()方法来获取想要的配置信息。
application.properties文件
server.port=8081

package com.example.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    @Autowired
    private Environment env;

    @GetMapping("/hellow")
    public String hello(){
        String point = env.getProperty("server.port");
        return point;
    }
}

输出
在这里插入图片描述
2.value.

  @Value("${server.port}")
    private String point;
    @GetMapping("/hellow")
    public String hello(){

        return point;
    }

3.@ConfigurationProperties ,prefix定义配置得前缀

package com.example.demo.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@ConfigurationProperties(prefix="com.test")
@Component
public class HelloConfig {

    private String name;
    private String node;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNode() {
        return node;
    }

    public void setNode(String node) {
        this.node = node;
    }
    public String showInfo(){
        return name+"--"+node;
    }
}

 @Autowired
    private HelloConfig con;
    @GetMapping("/hollow")
    public String hello(){
        return con.showInfo();
    }

输出
在这里插入图片描述
以上三种方法配置文件都是application.
4.自定义配置文件。用@PropertySource指定配置文件位置。这个注解不能读取.yml的配置文件,需要重新定义factory。
配置文件为hello.yml
在这里插入图片描述

package com.example.demo.config;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@PropertySource(value={"classpath:hello.yml"},factory = YmlConfigFactory.class)
@Component
@Data
public class HelloConfig {

    @Value("${com.test.name}")
    private String name;
    @Value("${com.test.node}")
    private String node;

   
    public String showInfo(){
        return name+"--"+node;
    }
}

重新定义工厂类如下

package com.example.demo.config;

import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.DefaultPropertySourceFactory;
import org.springframework.core.io.support.EncodedResource;

import java.io.IOException;
import java.util.Properties;

public class YmlConfigFactory extends DefaultPropertySourceFactory {
    @Override
    public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
        String sourceName = name != null ? name : resource.getResource().getFilename();
        if (!resource.getResource().exists()) {
            return new PropertiesPropertySource(sourceName, new Properties());
        } else if (sourceName.endsWith(".yml") || sourceName.endsWith(".yaml")) {
            Properties propertiesFromYaml = loadYml(resource);
            return new PropertiesPropertySource(sourceName, propertiesFromYaml);
        } else {
            return super.createPropertySource(name, resource);
        }
    }

    private Properties loadYml(EncodedResource resource) throws IOException {
        YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
        factory.setResources(resource.getResource());
        factory.afterPropertiesSet();
        return factory.getObject();
    }
}

@RestController
public class HelloController {
    @Autowired
    private HelloConfig con;
    @GetMapping("/hollow")
    public String hello(){
        return con.showInfo();
    }

}
输出
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值