1. 创建 maven 工程 , 在 pom 文件中添加依赖
<Parent> <MoupRoD> org.springframework.boot </proupid> <artifactid> spring-boot-starter-parent </artifactid> <versers> 1.5.9.release </versions> </parent> <pendencies> <pendency> <proupid> org.springfamework.boot </groupid> <artifactid> spring-sharter-starter-webeb-webeb </artharter> </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
paquete com.kelly.controller; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.enableautoconfiguration; import org.springframework.contextation.componentscan; import org.springframework.context.annotation.configuration;@configuration@EnableautOconfiguration }} paquete com.kelly.controller; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.enableautoconfiguration; import org.springframework.context.annotation.componentscan; import; importar; import org.springframework.context.annotation.configuration;@configuration@EnableautOconfiguration }} paquete com.kelly.controller; import org.springframework.beans.factory.annotation.value; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.responseBody; @ControllerPublic Class FirstController {@Value ("$ {test.name}") Nombre de cadena privada; @Value ("$ {test.password}") PRIVADA String Password; @RequestMapping ("/") @ResponseBody String Home () {return "¡Hola Springboot!"; } @RequestMapping ("/Hello") @ResponseBody String Hello () {return "Nombre:" + Nombre + "," + "Contraseña:" + contraseña; }}5. 打开浏览器 , 输入 http: // localhost: 8081/springboot/hola 即可看到结果
6. 使用 Java Bean 的方式读取自定义配置文件 Define. Propiedades
Defineartity.java
paquete com.kelly.entity; import org.springframework.boot.context.properties.configurationProperties; import org.springframework.context.annotation.propertySource; importar; importar; org. contraseña de cadena privada; public String getPName () {return pname; } public void setPName (string pname) {this.pname = pname; } public String getPassword () {return Password; } public void setPassword (String Password) {this.password = contraseña; }} SecondController.javapackage com.kelly.controller; import org.springframework.beans.factory.annotation.aUtowired; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.requestmappes; import org.springframework.web.bind.annotation.ResponseBody; import com.kelly.entity.defineEntity; @ControllerPublic Class SecondController {@auTowired DefineEntity DefineEntity; @RequestMapping ("/Define") @ResponseBody String Define () {return "test.name:" + defineEntity.getpName () + ", test.password:" + defineEntity.getPassword (); }}7. 打开浏览器 , 访问 访问 http: // localhost: 8081/springboot/define 可以看到输出结果 可以看到输出结果
补充 : 我的项目的目录结构
总结
以上所述是小编给大家介绍的 springboot 读取配置文件及自定义配置文件的方法 , 希望对大家有所帮助 如果大家有任何疑问请给我留言 小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持! 小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持!