1. 创建 Maven 工程 在 Pom 文件中添加依赖
<parent> <groupid> org.springframework.boot </GroupId> <ArtifactId> Spring-Boot-Starter-parent </artifactid> <sersive> 1.5.9.Release </version> </parent> <Dependencies> <Dependency> <groupid> org.springframework.boot </GroupId> <Artifactid> prings-boot-startrater-trame-weid-weid-weid-weid-weid-startr </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
пакет com.kelly.controller; import org.springframework.boot.springapplication; импорт org.springframework.boot.autoconfigure.enableautoconfiguration; импорт org.springframework.context.annotation.componentscan; импорт org.springframework.nannotation.componentscan; импорт org.springframework.context.annotation.configuration;@configuration@enableautoconfiguration // 自动加载配置信息 @componentscan ("com.kelly") // 使包路径下带有注解的类可以使用 @autowired 自动注入 public class startapplication {public void main (string [] args) {SpringApplication.run (startapplication {class.class.class.class.class. }} пакет com.kelly.controller; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.enableautoconfiguration; импорт org.springframework.context.annotation.componentscan; org.springframework.context.annotation.configuration;@configuration@enableautoconfiguration // 自动加载配置信息 @componentscan ("com.kelly") // 使包路径下带有注解的类可以使用 @autowired 自动注入 public class startapplication {public void main (string [] args) {SpringApplication.run (startapplication {class.class.class.class.class. }} пакет com.kelly.controller; import org.springframework.beans.factory.annotation.value; import org.springframework.stereotype.controller; импорт org.springframework.web.bind.annotation.requestmapping; import.springframe.web.bind.annotation.requestmapp org.springframework.web.bind.annotation.responsebody; @ControllerPublic Class FirstController {@Value ("$ {test.name}") Private String name; @Value ("$ {test.password}") private String пароль; @RequestMapping ("/") @Responsebody string home () {return "hello springboot!"; } @RequestMapping ("/hello") @Responsebody string hello () {return "name:" + name + "," + "пароль:" + password; }}5. 打开浏览器 , 输入 http: // localhost: 8081/springboot/hello 即可看到结果
6. 使用 Java Bean 的方式读取自定义配置文件 Определите.properties
Defineentity.java
пакет com.kelly.entity; import org.springframework.boot.context.properties.configurationProperties; Import org.springframework.context.annotation.propertySource; импорт org.springframework.stereotype.component;@component@configurationproperties (prefix = "deconetest")@propertysource ("classpath: define.properties") public class defineentity {private String pname; Private String Password; public String getPname () {return pname; } public void setPname (String pname) {this.pname = pname; } public String getPassword () {return пароль; } public void setPassword (String password) {this.password = password; }} SecondController.javapackage com.kelly.controller; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.controller; import org.springframework.web.bind.antation.requestmapping; 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 读取配置文件及自定义配置文件的方法 希望对大家有所帮助 , 如果大家有任何疑问请给我留言 , 小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持!