1. 创建 maven 工程, 在 pom 文件中添加依赖
<carent> <roupId> org.springframework.boot </groupId> <ratifactid> Spring-Boot-Starter-Parent </artifactid> <version> 1.5.9.release </เวอร์ชัน> </parent> <ArtIfactId> Spring-Boot-Starter-WEB </artifactId> </การพึ่งพาอาศัย> <!-单元测试使用-> <การพึ่งพา> <GroupId> org.springframework.boot </groupid> <Scope> ทดสอบ </cope> </derctency> </pendencies>
2. 创建项目启动类 startapplication.java
แพ็คเกจ com.kelly.controller; นำเข้า org.springframework.boot.springapplication; นำเข้า org.springframework.boot.autoconfigure.enableautoconfiguration นำเข้า org.springframework.context.annotation.ComponentsCan; org.springframework.context.annotation.configuration;@configuration@enableautoconfiguration // 自动加载配置信息 @componentscan ("com.kelly") // 使包路径下带有注解的类可以使用 @autowired 自动注入การเริ่มต้นคลาสสาธารณะ }} แพ็คเกจ com.kelly.controller; นำเข้า org.springframework.boot.springapplication; นำเข้า org.springframework.boot.autoconfigure.enableautoconfiguration; นำเข้า org.springframework.context.annotation org.springframework.context.annotation.configuration;@configuration@enableautoconfiguration // 自动加载配置信息 @componentscan ("com.kelly") // 使包路径下带有注解的类可以使用 @autowired 自动注入การเริ่มต้นคลาสสาธารณะ }} แพ็คเกจ com.kelly.controller; นำเข้า org.springframework.beans.factory.annotation.value; นำเข้า org.springframework.stereotype.controller; นำเข้า org.springframework.web.bind.annotation.requestmapping; org.springframework.web.bind.annotation.responsebody; @controllerpublic คลาส FirstController {@value ("$ {test.name}") ชื่อสตริงส่วนตัว; @Value ("$ {test.password}") รหัสผ่านสตริงส่วนตัว; @RequestMapping ("/") @ResponseBody String Home () {return "Hello Springboot!"; } @RequestMapping ("/hello") @ResponseBody String สวัสดี () {return "ชื่อ:" + ชื่อ + "," + "รหัสผ่าน:" + รหัสผ่าน; -5. 打开浏览器, 输入 http: // localhost: 8081/springboot/hello 即可看到结果
6. 使用 Java Bean 的方式读取自定义配置文件 define.properties
defineentity.java
แพ็คเกจ com.kelly.entity; นำเข้า org.springframework.boot.context.properties.configurationProperties; นำเข้า org.springframework.context.annotation.propertysource; org.springframework.stereotype.component;@component@configurationProperties (คำนำหน้า = "definetest")@propertySource ("classPath: define.properties") คลาสสาธารณะ defineentity {สตริงส่วนตัว pname; รหัสผ่านสตริงส่วนตัว สตริงสาธารณะ getPname () {return pname; } โมฆะสาธารณะ setPname (สตริง pName) {this.pname = pname; } สตริงสาธารณะ getPassword () {ส่งคืนรหัสผ่าน; } โมฆะสาธารณะ setPassword (รหัสผ่านสตริง) {this.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 คลาสที่สอง controller {@autowired defineentity defineentity; @RequestMapping ("/define") @ResponseBody String define () {return "test.name:" + defineentity.getPname () + ", test.pass.word:" + defineentity.getPassword (); -7. 打开浏览器, 访问 http: // localhost: 8081/springboot/define, 可以看到输出结果可以看到输出结果
补充: 我的项目的目录结构
总结
以上所述是小编给大家介绍的 Springboot 读取配置文件及自定义配置文件的方法,,, 如果大家有任何疑问请给我留言, 小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持!