栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot 加载配置文件的方式

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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();
    }

}
输出

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/275541.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号