Spring中依賴注入的四種方式,SpringBoot配置綁定的兩種方式

 2023-10-17 阅读 25 评论 0

摘要:SpringBoot配置綁定的兩種方式 演示文件 bean public class Student {private String name;private Integer age;public Student(){}public Student(String name, Integer age) {this.name = name;this.age = age;}public String getName() {return name;}public void

SpringBoot配置綁定的兩種方式

演示文件

bean

public class Student {private String name;private Integer age;public Student(){}public Student(String name, Integer age) {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;}@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", age=" + age +'}';}
}

properties
在這里插入圖片描述

方式一(@Component+@ConfigurationProperties)

將@Component和@ConfigurationProperties都標在Student類上,@ConfigurationProperties(prefix = “stu”)中的prefix = "stu"表示將從配置文件中前綴為stu的配置項開始找。
在這里插入圖片描述

方式二(@ConfigurationProperties+@EnableConfigurationProperties)

@ConfigurationProperties標在Student上,@EnableConfigurationProperties標在配置類上,Student在配置類中造。
Student類
在這里插入圖片描述

Spring中依賴注入的四種方式。配置類
在這里插入圖片描述

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/142743.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息