一、SpringBoot配置

一、SpringBoot配置

一、在 application.yml 写入如下配置内容

person:
  name: taven
  age: 22
 

二、定义 PersonProperties.java 文件

package com.taven.demo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "person")
public class PersonProperties {

    private String name;
    
    private int age;
   
    public int getAge() {
        return age;
    }

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

    public String getName() {
        return name;
    }

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

}

三、定义PropertiesController 用来注入 PersonProperties

import com.taven.demo.PersonProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RequestMapping("/properties")
@RestController
public class PropertiesController {

    private static final Logger log = LoggerFactory.getLogger(PropertiesController.class);

    private final PersonProperties personProperties;

    @Autowired
    public PropertiesController(PersonProperties personProperties) {
        this.personProperties = personProperties;
    }

    @GetMapping("/person")
    public PersonProperties personProperties() {
        log.info(personProperties.getName());
        return personProperties;

    }
}
hmoban主题是根据ripro二开的主题,极致后台体验,无插件,集成会员系统
自学咖网 » 一、SpringBoot配置