Springboot讀取配置文件及自定義配置文件的方法
1.創(chuàng)建maven工程,在pom文件中添加依賴
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 單元測(cè)試使用 -->
<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.創(chuàng)建項(xiàng)目啟動(dòng)類 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
@EnableAutoConfiguration //自動(dòng)加載配置信息
@ComponentScan("com.kelly")//使包路徑下帶有注解的類可以使用@Autowired自動(dòng)注入
public class StartApplication {
public static void main(String[] args) {
SpringApplication.run(StartApplication.class, args);
}
}
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
@EnableAutoConfiguration //自動(dòng)加載配置信息
@ComponentScan("com.kelly")//使包路徑下帶有注解的類可以使用@Autowired自動(dòng)注入
public class StartApplication {
public static void main(String[] args) {
SpringApplication.run(StartApplication.class, args);
}
}
package 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;
@Controller
public class FirstController {
@Value("${test.name}")
private String name;
@Value("${test.password}")
private String password;
@RequestMapping("/")
@ResponseBody
String home()
{
return "Hello Springboot!";
}
@RequestMapping("/hello")
@ResponseBody
String hello()
{
return "name: " + name + ", " + "password: " + password;
}
}
5.打開(kāi)瀏覽器,輸入 http://localhost:8081/springboot/hello 即可看到結(jié)果

6.使用java bean的方式讀取自定義配置文件 define.properties
DefineEntity.java
package com.kelly.entity;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix="defineTest")
@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 password;
}
public void setPassword(String password) {
this.password = password;
}
}
SecondController.java
package com.kelly.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.kelly.entity.DefineEntity;
@Controller
public class SecondController {
@Autowired
DefineEntity defineEntity;
@RequestMapping("/define")
@ResponseBody
String define()
{
return "test.name:" + defineEntity.getPname() + ", test.password:" + defineEntity.getPassword();
}
}
7.打開(kāi)瀏覽器,訪問(wèn) http://localhost:8081/springboot/define,可以看到輸出結(jié)果

補(bǔ)充:我的項(xiàng)目的目錄結(jié)構(gòu)

總結(jié)
以上所述是小編給大家介紹的Springboot讀取配置文件及自定義配置文件的方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
mybatis攔截器實(shí)現(xiàn)數(shù)據(jù)庫(kù)數(shù)據(jù)權(quán)限隔離方式
通過(guò)Mybatis攔截器,在執(zhí)行SQL前添加條件實(shí)現(xiàn)數(shù)據(jù)權(quán)限隔離,特別是對(duì)于存在用戶ID區(qū)分的表,攔截器會(huì)自動(dòng)添加如user_id=#{userId}的條件,確保SQL在執(zhí)行時(shí)只能操作指定用戶的數(shù)據(jù),此方法主要應(yīng)用于Mybatis的四個(gè)階段2024-11-11
springboot多數(shù)據(jù)源配合docker部署mysql主從實(shí)現(xiàn)讀寫(xiě)分離效果
這篇文章主要介紹了springboot多數(shù)據(jù)源配合docker部署mysql主從實(shí)現(xiàn)讀寫(xiě)分離,通過(guò)使用docker獲取mysql鏡像,具體內(nèi)容詳情跟隨小編一起看看吧2021-09-09
詳解獲取Spring MVC中所有RequestMapping以及對(duì)應(yīng)方法和參數(shù)
本篇文章主要介紹了詳解獲取Spring MVC中所有RequestMapping以及對(duì)應(yīng)方法和參數(shù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03
SpringBoot整合OpenCV的實(shí)現(xiàn)示例
這篇文章主要介紹了SpringBoot整合OpenCV的實(shí)現(xiàn)示例。文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
JAVA8發(fā)送帶有Body的HTTP GET請(qǐng)求
本文主要介紹了JAVA8發(fā)送帶有Body的HTTP GET請(qǐng)求,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
SpringBoot實(shí)現(xiàn)輕量級(jí)動(dòng)態(tài)定時(shí)任務(wù)管控及組件化的操作步驟
文章介紹了一種在SpringBoot中實(shí)現(xiàn)動(dòng)態(tài)定時(shí)任務(wù)的解決方案,基于COLA架構(gòu)理論,封裝到了組件層,該組件支持類級(jí)別和方法級(jí)別的定時(shí)任務(wù)注冊(cè),并提供了易用性和擴(kuò)展性,組件使用Maven形式引入,并且可以通過(guò)YAML配置文件進(jìn)行設(shè)置,感興趣的朋友一起看看吧2024-11-11
javaweb登錄驗(yàn)證碼的實(shí)現(xiàn)方法
這篇文章主要為大家詳細(xì)介紹了javaweb登錄驗(yàn)證碼的實(shí)現(xiàn)方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11

