1. 创建 Maven 工程 , 在 pom 文件中添加依赖
<Ters </GroupId> org.springFramework.boot </GroupId> <artifactId> Spring-Boot-Starter-Parent </artifactId> <version> 1.9.Release </Version> </parent> <dEpdern> <abhängigkeit> <Crogid> org. </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
Paket com.kelly.controller; import org.springframework.boot.springApplication; import org.springframework.boot.autoconFigure.enableAutoconfiguration; import org.springframework.context.annotation.comPonentscan; org.springframework.context.annotation.configuration;@configuration@EnableAutoConfiguration // 自动加载配置信息 @Componentscan ("com.kelly") // 使包路径下带有注解的类可以使用 @autowired 自动注入 öffentliche Klasse StartApplication {Public Static void main (String [] args) {SpringApplication.run (startApplication.Cls). }} Paket com.kelly.controller; import org.springframework.boot.springApplication; org.springframework.boot.autoconFigure.EnableAutoConFiguration; org.springframework.context.annotation.configuration;@configuration@EnableAutoConfiguration // 自动加载配置信息 @Componentscan ("com.kelly") // 使包路径下带有注解的类可以使用 @autowired 自动注入 öffentliche Klasse StartApplication {Public Static void main (String [] args) {SpringApplication.run (startApplication.Cls). }} paket com.kelly.controller; import org.springframework.bean.factory.annotation org.springframework.web.bind.annotation.ResponseBody; @ControllerPublic Class FirstController {@Value ("$ {test.name}") privater String -Name; @Value ("$ {test.password}") privates Zeichenfolgenkennwort; @RequestMapping ("/") @ResponseBody String home () {return "Hallo Springboot!"; } @RequestMapping ("/hello") @Responbody String hello () {return "Name:" + name + "," + "Passwort:" + Passwort; }}5. 打开浏览器 , 输入 http: // localhost: 8081/Springboot/Hallo 即可看到结果
6. 使用 Java Bean 的方式读取自定义配置文件 Define.Properties
DefineEntity.java
Paket com.kelly.entity; import org.springframework.boot.context.properties.configurationProperties; org.springFramework.stereotype.comPonent;@component@configurationProperties (Präfix = "DefinitionStest")@PropertySource ("classPath: Define.Properties") öffentliche Klasse DefineEventity {private String pname; privates Zeichenfolgenkennwort; public String getPname () {return pName; } public void setpname (String pName) {this.pname = pname; } public String getPassword () {Kennwort zurückgeben; } public void setPassword (String -Passwort) {this.password = password; }} SecondController.javapackage com.kelly.controller; import org.springframework.bean.factory.annotation org.springframework.web.bind.annotation.Responsebody; import com.kelly.entity.DefineEtity; @ControllerPublic Class SecondController {@autowired DefineEntity DefineEventity; @RequestMapping ("/define") @ResponseBody String define () {return "test.name:" + defineEntity.getPname () + ", test.password:" + defineEntity.getPassword (); }}7. 打开浏览器 , 访问 http: // localhost: 8081/Springboot/Define , 可以看到输出结果
补充 : 我的项目的目录结构
总结
以上所述是小编给大家介绍的 Springboot 读取配置文件及自定义配置文件的方法 , 希望对大家有所帮助 , 如果大家有任何疑问请给我留言 , 小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持! 小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持! 小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持!