1. 创建 maven 工程 , 在 pom 文件中添加依赖
<Parent> <puperiD> org.springframework.boot </frupiD> <TRATIFACTID> Spring-boot-Starter-Parent </ArtifactId> <Versão> 1.5.9.Release </sipers> </eracer> </dependências> <pendency> <purgid> org.springFramework.Boot </Grupoid> Artift> </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
pacote com.kelly.controller; importar org.springframework.boot.springApplication; importar org.springframework.boot.autoconfigure.enableautoconfiguration; importação org.springframeworkscontext.annotation.componEntsCan; importação; org.springframework.context.annotation.configuration;@configuration@enableautoconfiguration // 自动加载配置信息 @componentsCan ("com.kelly") // 使包路径下带有注解的类可以使用 @autowired 自动注入 public startApplication {public static void main (string [] args) {springApplication.rtation (public StartApp Maid (] args); }} pacote com.kelly.controller; importar org.springframework.boot.springApplication; importar org.springframework.boot.autoconfigure.enableautoconfiguration; importar org.springframework.Context.annotation.comPoNEntsCansCon org.springframework.context.annotation.configuration;@configuration@enableautoconfiguration // 自动加载配置信息 @componentsCan ("com.kelly") // 使包路径下带有注解的类可以使用 @autowired 自动注入 public startApplication {public static void main (string [] args) {springApplication.rtation (public StartApp Maid (] args); }} pacote com.kelly.controller; importar org.springframework.beans.factory.annotation.value; importar org.springframework.tereotype.controller; importar org.springframework.web.bind.annotation.requestMapping; importar; importar; org.springframework.web.bind.annotation.ResponseBody; @ControllerPublic Classe FirstController {@Value ("$ {test.name}") nome da string privada; @Value ("$ {test.password}") senha de sequência privada; @RequestMapping ("/") @ResponseBody String home () {return "Hello Springboot!"; } @RequestMapping ("/hello") @ResponseBody String hello () {return "Name:" + Name + "," + "Senha:" + Senha; }}5. 打开浏览器 , 输入 http: // localhost: 8081/springboot/hello 即可看到结果
6. 使用 Java Bean 的方式读取自定义配置文件 Definir.Properties
Defineentity.java
pacote com.kelly.entity; importar org.springframework.boot.context.properties.configurationproperties; importar org.springframework.context.annotation.propertysource; org.springframework.stereotype.component;@component@ConfigurationProperties (prefix = "DefeneTest")@PropertySource ("ClassPath: define.properties") classe pública Defineentity {private string pname; senha de sequência privada; public string getpName () {return pname; } public void setpName (string pname) {this.pname = pname; } public string getPassword () {return senha; } public void setPassword (string senha) {this.password = senha; }} SecondController.javapackage com.kelly.controller; importar org.springframework.beans.factory.annotation.autowired; importar org.springframework.tereotype.controller; importar org.springframework.web.brind.anotation.requesttation.requestration; org.springframework.web.bind.annotation.ResponseBody; importar com.kelly.entity.defineentity; @ControllerPublic Class SecondController {@AUTOWIRED Definatity DefinEentity; @RequestMapping ("/define") @ResponseBody String define () {return }}7. 打开浏览器 , 访问 http: // localhost: 8081/springboot/define , 可以看到输出结果
补充: 我的项目的目录结构
总结
以上所述是小编给大家介绍的 Springboot 读取配置文件及自定义配置文件的方法 , 希望对大家有所帮助 , 如果大家有任何疑问请给我留言 , 小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持!