1. 创建 Maven 工程 , 在 Pom 文件中添加依赖
<parent> <proupId> org.springframework.boot </rombasid> <ArtifactId> printemps-boot-starter-parent </retifactid> <version> 1.5.9.release </-version> </parestades> <dependency> <proupId> org.springframework.boot </prountid> <ArtifActid> Spring-boot-starterweb </prountid> <artifactid> </dependency> <!-- 单元测试使用 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> </dependencies>
2. 创建项目启动类 startApplication.java
package com.kelly.controller; import org.springframework.boot.springApplication; import org.springframework.boot.autoconfigure.enableAutoConfiguration; import org.springframework.context.annotation.comPonentsCan; import org.springframework.context.annotation.configuration; @ configuration @ perteAutoConfiguration // 自动加载配置信息 @componentscan ("com.kelly") // 使包路径下带有注解的类可以使用 @autowired 自动注入 public class startApplication {public static void main (string [] args) {printempsApplication.run (startApplication.Class, args); }} package com.kelly.controller; import org.springframework.boot.springApplication; import org.springframework.boot.autoconfigure.enableAutoConfiguration; import org.springframework.context.annotation. org.springframework.context.annotation.configuration; @ configuration @ perteAutoConfiguration // 自动加载配置信息 @componentscan ("com.kelly") // 使包路径下带有注解的类可以使用 @autowired 自动注入 public class startApplication {public static void main (string [] args) {printempsApplication.run (startApplication.Class, args); }} package com.kelly.coller; org.springframework.web.bind.annotation.responsebody; @ControllerPublic classe FirstController {@Value ("$ {test.name}") Nom de chaîne privée; @Value ("$ {test.password}") Mot de passe de chaîne privée; @RequestMapping ("/") @ResponseBody String home () {return "Hello Springboot!"; } @RequestMapping ("/ Hello") Hello Hello () {RETOUR "Name:" + Name + "," + "Mot de passe:" + Mot de passe; }}5. 打开浏览器 , 输入 http: // localhost: 8081 / Springboot / Hello 即可看到结果
6. 使用 Java Bean 的方式读取自定义配置文件 Définir.Properties
Defineentity.java
package com.kelly.entity; import org.springframework.boot.context.properties.configurationproperties; import org.springframework.context.annotation.propertysource; org.springframework.sterreoType.Component; @ composant @ configurationProperties (prefix = "Defitest") @ propriétéSource ("classPath: define.properties") public class Defineentity {private String pname; mot de passe de chaîne privé; public String getpname () {return pName; } public void setpname (String pname) {this.pname = pname; } public String getPassword () {return mot de passe; } public void setPassword (String Motword) {this.password = mot de passe; }} SecondController.javapackage com.kelly.controller; import org.springframework.beans.factory.annotation.autowired; import org.springframework.sterreotype.controller; import org.springframework.web.bind.annotation.requestmapping; import; org.springframework.web.bind.annotation.Responsebody; import com.kelly.entity.defineentity; @ControllerPublic class SecondController {@autowired Defineentity Defineentity; @RequestMapping ("/ define") String de laResponseBody Define () {return "test.name:" + defineentity.getpname () + ", test.password:" + defineentity.getPassword (); }}7. 打开浏览器 , 访问 http: // localhost: 8081 / Springboot / Define , 可以看到输出结果
补充 : 我的项目的目录结构
总结
以上所述是小编给大家介绍的 Springboot 读取配置文件及自定义配置文件的方法 , 希望对大家有所帮助 , 如果大家有任何疑问请给我留言 , 小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持!