温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

springboot中怎么配置文件绑定

发布时间:2021-08-07 14:06:39 来源:亿速云 阅读:94 作者:Leah 栏目:编程语言

这期内容当中小编将会给大家带来有关springboot中怎么配置文件绑定,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

先创建一个peron类,然后需要注解configurationProperties(prefix ="person")<br data-filtered="filtered">然后需要加一个@component<br data-filtered="filtered">因为只有在springboot的容器才能提供容器提供的@configurationProperties<br data-filtered="filtered">@Component@ConfigurationProperties(prefix = "person")public class Person {  private String lastName;  private Integer age;  private boolean boss;  private Date birth;  private Map<String,Object> maps;  private List<Object> lists;  private Dog dog;  public String getLastName() {    return lastName;  }  public void setLastName(String lastName) {    this.lastName = lastName;  }  public Integer getAge() {    return age;  }  public void setAge(Integer age) {    this.age = age;  }  public boolean isBoss() {    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=" + lastName + ", age=" + age + ", boss=" + boss + ", birth=" + birth + ", maps="        + maps + ", lists=" + lists + ", dog=" + dog + "]";  }   }

dog类  

public class Dog {  private String Name;  private Integer age;  public String getName() {    return Name;  }  public void setName(String name) {    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 + "]";  }   }

写完后,ide会提示需要在pom.xml中导入组件处理器。

<!-- 配置文件的处理器 ,配置文件进行绑定就会有提示-->    <dependency>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-configuration-processor</artifactId>      <optional>true</optional>    </dependency>  </dependencies>

然后创建配置文件,有两种方式,一个时yml文件,另一个时properties

1,application.yml

person: last-name: zhangsan age: 24 boss: false birth: 2017/12/5 maps: {k1: v1,k2: v2} lists: [lisi, zhangsan] dog: Name: xiaohei age: 4

2.application.properties

中文字,在eclipse中自动转为unicode码

person.age=24person.last-name=\u5F20\u4E09person.birth=2000/1/1person.boss=falseperson.maps.k1=value1person.maps.k2=12person.dog.name=\u5C0F\u9ED1person.dog.age=2

在test中使用spring boot的单元测试

@RunWith(SpringRunner.class)@SpringBootTestclass Helloworld01QuickApplicationTests {  @Autowired  Person person;  @Test  void contextLoads() {    System.out.println(person);  }}

运行,会看到得到配置文件中的数据

在获取配置文件中注入值得时候,可以使用@value,也可以使用@configurationProperties;

如果只是在逻辑中获取一下配置文件中得值,那么就使用@value

在配置文件注入值得时候也可以校验

在类加入注解@validate

配置文件注入数据校验

@validatepublic class person{@Emailprivate String last-name;....  }

@PropertySource("classpath:person.properties") :加载指定的配置文件

@ImportResource(“classpath:beans.xml”):导入spring配置文件,让配置文件生效;

springboot推荐给容器增加组件

1.配置类--》spring配置文件

2.使用@bean给容器中增加组件;

配置文件占位符

1.随机数

${random.value}、${random.int}、${random.long}${random.int(10)}、${random.int[1024,65536]}

2.配置文件中找不到属性时的默认值。

${app.name:金毛}来指定找不到属性时的默认值。

profile

1.多个profile文件

Profile是Spring对不同环境提供不同配置功能的支持,可以通过激活、指定参数等方式快速切换环境

一般我们在开发的时候有测试环境,开发环境等。

我们在编写多个配置文件的时候,文件名字是application-(profile).properties/yml(这二种格式的都行)。

默认使用application.properties.

2.yml支持多文档块方式

application.yml

#三个横线属于一个文档块#激活哪个环境spring: profiles:  active: test #测试环境---server: port: 8081spring: profiles: test #开发环境---server: port: 8082spring: profiles: dev

3.激活指定profile

在配置文件中指定spring.profiles.active =dev

springboot配置文件加载位置

这些配置都会加载,然后进行互补配置。

上述就是小编为大家分享的springboot中怎么配置文件绑定了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI