conf.yml

name: jennie
age: 18
#	friends: 

bean(Student)

package com.fr.springboot1.properties;

import java.util.Map;

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

@Component
@PropertySource("classpath:conf.yml")
@ConfigurationProperties()
public class Student {
	private String name;
	private String age;
	private Map<String,Object> friends;

	public String getName() {
		return name;
	}

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

	public String getAge() {
		return age;
	}

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

	public Map<String, Object> getFriends() {
		return friends;
	}

	public void setFriends(Map<String, Object> friends) {
		this.friends = friends;
	}


}

Controller(StudentController)

package com.fr.springboot1.controller;

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

import com.fr.springboot1.properties.Student;

@RestController
public class StudentController {

	private Student student;

	
	@Autowired
	public StudentController(Student student) {
		this.student = student;
	}

	@RequestMapping("/student")	
	public Student student() {
		return student;
	}

}

测试结果:

springboot自定义配置文件(yml)

相关文章:

  • 2021-12-13
  • 2021-09-08
  • 2019-06-17
  • 2021-07-22
  • 2021-03-02
  • 2021-12-03
  • 2021-11-15
  • 2018-11-24
猜你喜欢
  • 2017-12-12
  • 2019-06-16
  • 2021-12-03
  • 2021-12-03
  • 2019-11-07
  • 2019-12-24
  • 2018-01-08
  • 2021-11-13
相关资源
相似解决方案