yaml中自定义属性值

方法1:
yam:

server:
  port: 8081
  servlet:
    context-path: /hello
name: 小伙子
age: 12

java:

package com.yl.ceshi.ceshi;

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

/**
 * @author youzo
 */
@RestController
public class Ceshi {
    @Value("${name}")
    private String name;
    @Value("${age}")
    private Integer age;

    @RequestMapping("/hello")
    public String hello(){
        System.out.println("name{}"+name);
        System.out.println("age{}"+age);
        return name+age;
    }
}

前端请求展示:
yaml中自定义属性值_第1张图片
方法2:由于方法1中的方法过于臃肿,我们采取方法2
yaml:

server:
  port: 8081
  servlet:
    context-path: /hello
school:
  name: 小伙子
  age: 12

java->bean:

package com.yl.ceshi.ceshi;

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

/**
 * @author youzo
 */

@Component
@ConfigurationProperties(prefix = "school")
@Data
public class School {
    private String name;
    private Integer age;
}

java:

package com.yl.ceshi.ceshi;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author youzo
 */
@RestController
public class Ceshi {

    @Autowired
    private School school;

    @RequestMapping("/hello")
    public String hello(){
        System.out.println("name{}"+school.getName());
        System.out.println("age{}"+school.getAge());
        return school.getName()+school.getAge();
    }
}

前端页面请求:
在这里插入图片描述

你可能感兴趣的:(实用的处理小技巧)