1. 创建 maven 工程 , 在 pom 文件中添加依赖
<Arnal> <roupl> org.springframework.boot </groupId> <intifactid> Spring-boot-starter-parent </shintifactid> <splement> 1.5.9 <ChotifactId> Spring-boot-starter-web </stifactid> </sependency> <!-单元测试使用-> <reperency> <rouplencid> org.springframework.boot </rougiD> <StifactId> spring-boot-starter-test </intifactid> </sempendency> <scope> اختبار </scope> </reperency> </تبعية>
2. 创建项目启动类 startapplication.java
package com.kelly.controller ؛ استيراد org.springframework.boot.springapplication ؛ استيراد org.springframework.boot.autoconfigure.enableautoconfiguration ؛ استيراد org.springframework.context.antation.componentscan ؛ استيراد org.springframework.context.annotation.configuration ؛@configuration@enableeautoconFiguration // 自动加载配置信息Componentscan ("com.kelly") // 使包路径下带有注解的类可以使用 使包路径下带有注解的类可以使用UTOWIRE 自动注入 startapplication {public static void main (string) {springapplication.class }} package com.kelly.controller ؛ استيراد org.springframework.boot.springapplication ؛ استيراد org.springframework.boot.autoconfigure.enabeautoconfiguration ؛ استيراد org.springframework.context.annotation.componentscan ؛ org.springframework.context.annotation.configuration ؛@configuration@enableeautoconFiguration // 自动加载配置信息Componentscan ("com.kelly") // 使包路径下带有注解的类可以使用 使包路径下带有注解的类可以使用UTOWIRE 自动注入 startapplication {public static void main (string) {springapplication.class }} package com.kelly.controller ؛ استيراد org.springframework.beans.factory.annotation.value ؛ استيراد org.springframework.stereopee.controller ؛ استيراد org.springframework.web.bind.annotation.requestMapp org.springframework.web.bind.annotation.ResponseBody ؛ controllerpublic class firstController {value ("$ {test.name}") اسم السلسلة الخاص ؛ Value ("$ {test.password}") كلمة مرور السلسلة الخاصة ؛ requestmapping ("/") responsebody string home () {return "hello springboot!" ؛ } @requestmapping ("/hello") @responsebody string hello () {return "name:" + name + "،" + "password:" + password ؛ }}5. 打开浏览器 , 输入 http: // localhost: 8081/springboot/hello 即可看到结果
6. 使用 Java Bean 的方式读取自定义配置文件 define.properties
defineentity.java
package com.kelly.entity ؛ استيراد org.springframework.boot.context.properties.configurationProperties ؛ استيراد org.springframework.context.annotation.propertysource ؛ استيراد org.springframework.stereotype.component ؛@component@configurationProperties (prefix = "definetest")@propertySource ("classpath: define.properties") class public class {private string pname ؛ كلمة مرور السلسلة الخاصة ؛ السلسلة العامة getPname () {return pname ؛ } public void setPname (string pname) {this.pname = pname ؛ } السلسلة العامة getPassword () {return password ؛ } public void setPassword (سلسلة كلمة مرور) {this.password = password ؛ }} secondController.javapackage com.kelly.controller ؛ استيراد org.springframework.beans.factory.annotation.autowired ؛ استيراد org.springframework.stereotype.controller org.springframework.web.bind.annotation.ResponseBody ؛ استيراد com.kelly.entity.defineentity ؛ controllerpublic class secondController {autowired defineentity ؛ requestmapping ("/define") reresponsebody string define () {return "test.name:" + defineentity.getPname () + "، test.password:" + defineentity.getPassword () ؛ }}7. 打开浏览器 , 访问 http: // localhost: 8081/springboot/define , 可以看到输出结果
: : 我的项目的目录结构
总结
springboot 读取配置文件及自定义配置文件的方法 , 希望对大家有所帮助 , 如果大家有任何疑问请给我留言 , 小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持!