朱皮特的博客 自由的飞翔

springboot极简教程003-使用配置

2018-10-28
朱皮特
阅读量:

修改配置

在application.yml中添加您想要配置的数据,例如以下配置内容:

student:
  name: Jim
  age: 22
content: "name: ${student.name}, age: ${student.age}"

如何在Java代码中使用这些配置的数据呢?

@EnableAutoConfiguration

首先要使用该注解,该注解的作用可以搜索之,此处暂不详解。

方式一:使用注解@Value

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@EnableAutoConfiguration
public class HelloController {

    @Value("${student.name}")
    private String name;

    @Value("${student.age}")
    private Integer age;

    @Value("${content}")
    private String content;

    @RequestMapping("/")
    private String index() {
        return content;
    }

    @RequestMapping("/index")
    private String index2() {
        return String.format("student: %s, age: %d\n", this.name, this.age);
    }
}

方式一:使用注解@ConfigurationProperties、@PropertySource、@Autowired

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

@Component
@ConfigurationProperties(prefix = "student")
public class StudentProperties {
    private String name;
    private Integer age;

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

@PropertySource指定配置文件

例如在resources目录下创建test.yml用于存放测试数据:

student:
  name: Jim(Test)
  age: 22(Test)

使用该配置文件的数据,可以用@PropertySource指定使用的配置文件:

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@ConfigurationProperties(prefix = "student")
@PropertySource("classpath:test.yml")
public class StudentTestBean {
    private String name;
    private Integer age;

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

使用注解@Autowired进行自动装配

@RestController
@EnableAutoConfiguration
public class HelloController {
    @Value("${version}")
    private String version;

    @Autowired
    private StudentProperties student_default;

    @Autowired
    private StudentTestBean student_test;

    @RequestMapping("/student")
    private String student(String name, Integer age) {
        if (name == null) {
            name = student_default.getName();
        }
        if (age == null) {
            age = student_default.getAge();
        }
        return String.format("Online: %s\nname: %s, age: %d\nname(Test): %s, age(Test): %d", version, name, age, student_test.getName(), student_test.getAge());
    }

}

上一篇 springboot汇总

Comments

Content