Spring Boot自定义配置

开发配置:

  • IntelliJ Idea
  • JDK 1.8.0.131 64-bit
  • spring boot 1.5.8

1.新建自定义配置文件

student:
    name: Jack
    age: 16
    score:
        math: 90
        english: 80

Spring Boot自定义配置_第1张图片

2.新建配置文件对应的类

package com.example.demo.config;

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

@Component
@ConfigurationProperties(prefix = "student")
@PropertySource(value = "classpath:config/my-config.yml",factory=YamlPropertySourceFactory.class)//指定factory
public class Student {
    private String name;
    private Integer age;
    public Score score;

    public  static  class  Score{
        private Integer math;
        private Integer english;

        public Integer getMath() {
            return math;
        }

        public void setMath(Integer math) {
            this.math = math;
        }

        public Integer getEnglish() {
            return english;
        }

        public void setEnglish(Integer english) {
            this.english = english;
        }
    }

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Score getScore() {
        return score;
    }

    public void setScore(Score score) {
        this.score = score;
    }
}

package com.example.demo.config;

import org.springframework.boot.env.PropertySourcesLoader;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertySourceFactory;

import java.io.IOException;

public class YamlPropertySourceFactory implements PropertySourceFactory {
    @Override
    public PropertySource createPropertySource(String name, EncodedResource resource) throws IOException {
        return name != null ? new PropertySourcesLoader().load(resource.getResource(), name, null) : new PropertySourcesLoader().load(
                resource.getResource(), getNameForResource(resource.getResource()), null);
    }

    private static String getNameForResource(Resource resource) {
        String name = resource.getDescription();
        if (!org.springframework.util.StringUtils.hasText(name)) {
            name = resource.getClass().getSimpleName() + "@" + System.identityHashCode(resource);
        }
        return name;
    }
}

Spring Boot自定义配置_第2张图片

3.在程序中使用自定义配置

package com.example.demo.controller;

import com.example.demo.config.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 
 * @author Aaron
 *
 */
@Controller
public class IndexController {
    @Autowired
    private Student student;

    @RequestMapping(value = "/")
    @ResponseBody
    public String getBasicCode() {
        System.out.println("Student name:"+student.getName());
        System.out.println("Student age:"+student.getAge());
        System.out.println("Student score(math):"+student.getScore().getMath());
        System.out.println("Student score(english):"+student.getScore().getEnglish());
        return "index";
    }
}

访问localhost:8080/,Console显示如下:
Spring Boot自定义配置_第3张图片

参考:
https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#configuration-metadata-annotation-processor


你可能感兴趣的:(spring-boot)