java框架之Spring boot二:SpringBoot配置获取
Posted 爸爸去哪了2之熊猫三胞胎
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java框架之Spring boot二:SpringBoot配置获取相关的知识,希望对你有一定的参考价值。
java框架之Spring boot二:SpringBoot配置获取
resources文件夹中的目录结构:
static:保存所有的静态资源;js,css,images
templates:保存所有的模板页面;
application.properties:配置文件,可以修改一些默认配置
配置文件的作用:修改SpringBoot自动配置的默认值;SpringBoot在底层都给我们自动配置好
配置文件书写类型主要有两中,一种为properties,一种是yaml。这边是使用properties。
配置文件的书写:
server.port=8081
#idea使用的是UTF-8,properties使用的ascll码,所以会乱码
#Person
person.name=lala
person.age=18
person.birth=2017/12/15
person.boss=false
person.maps.k1=v1
person.maps.k2=15
person.lists=a,b,c
person.dog.name=dog
person.dog.age=15
对应bean类的书写,Person:
package com.example.demo.bean;
import java.util.Date;
import java.util.Map;
import java.util.List;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix="person")
public class Person
/**/
private String name;
private Integer age;
private Boolean boss;
private Date birth;
private Map<String, Object> maps;
private List<Object> lists;
private Dog dog;
public Person()
super();
public Person(String name, Integer age, Boolean boss, Date birth, Map<String, Object> maps, List<Object> lists,
Dog dog)
super();
this.name = name;
this.age = age;
this.boss = boss;
this.birth = birth;
this.maps = maps;
this.lists = lists;
this.dog = dog;
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 Boolean getBoss()
return boss;
public void setBoss(Boolean boss)
this.boss = boss;
public Date getBirth()
return birth;
public void setBirth(Date birth)
this.birth = birth;
public Map<String, Object> getMaps()
return maps;
public void setMaps(Map<String, Object> maps)
this.maps = maps;
public List<Object> getLists()
return lists;
public void setLists(List<Object> lists)
this.lists = lists;
public Dog getDog()
return dog;
public void setDog(Dog dog)
this.dog = dog;
@Override
public String toString()
return "Person [lastName=" + name + ", age=" + age + ", boss=" + boss + ", birth=" + birth + ", maps="
+ maps + ", lists=" + lists + ", dog=" + dog + "]";
对应bean类的书写,Dog:
package com.example.demo.bean;
public class Dog
private String name;
private Integer age;
public Dog()
super();
public Dog(String name, Integer age)
super();
this.name = name;
this.age = 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;
@Override
public String toString()
return "Dog [name=" + name + ", age=" + age + "]";
实现类的书写:
package com.example.demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.bean.Person;
@RestController
public class PersonController
@Autowired
private Person person;
@RequestMapping("person")
public String person()
return person.toString();
现在我们来说明一下,实现类的两个注解:
@Component:@component就是说把这个类交给Spring管理
@ConfigurationProperties(prefix=”person”):使用@ConfigurationProperties,它可以把同类的配置信息自动封装成实体类
以上是关于java框架之Spring boot二:SpringBoot配置获取的主要内容,如果未能解决你的问题,请参考以下文章
spring boot框架学习学前掌握之重要注解-通过java的配置方式进行配置spring
spring boot框架学习学前掌握之重要注解-通过java的配置方式进行配置spring
Spring Boot 2.2.0,性能提升+支持Java13
Java之Spring Boot入门到精通IDEA版SpringBoot整合其他框架Junit,Redis,MyBatis(一篇文章精通系列)中