最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Springboot讀取配置文件及自定義配置文件的方法

 更新時間:2017年12月13日 08:43:28   作者:Java從入門到跑路  
這篇文章主要介紹了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>
  <!-- 單元測試使用 -->
  <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)目啟動類 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 //自動加載配置信息
@ComponentScan("com.kelly")//使包路徑下帶有注解的類可以使用@Autowired自動注入
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 //自動加載配置信息
@ComponentScan("com.kelly")//使包路徑下帶有注解的類可以使用@Autowired自動注入
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.打開瀏覽器,輸入 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.打開瀏覽器,訪問 http://localhost:8081/springboot/define,可以看到輸出結(jié)果

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

總結(jié)

以上所述是小編給大家介紹的Springboot讀取配置文件及自定義配置文件的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • Spring-cloud Feign 的深入理解

    Spring-cloud Feign 的深入理解

    這篇文章主要介紹了Spring-cloud Feign 的深入理解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-02-02
  • java字符串格式化(String類format方法)

    java字符串格式化(String類format方法)

    這篇文章主要介紹了java字符串格式化(String類format方法),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • mybatis攔截器實(shí)現(xiàn)數(shù)據(jù)庫數(shù)據(jù)權(quán)限隔離方式

    mybatis攔截器實(shí)現(xiàn)數(shù)據(jù)庫數(shù)據(jù)權(quán)限隔離方式

    通過Mybatis攔截器,在執(zhí)行SQL前添加條件實(shí)現(xiàn)數(shù)據(jù)權(quán)限隔離,特別是對于存在用戶ID區(qū)分的表,攔截器會自動添加如user_id=#{userId}的條件,確保SQL在執(zhí)行時只能操作指定用戶的數(shù)據(jù),此方法主要應(yīng)用于Mybatis的四個階段
    2024-11-11
  • springboot多數(shù)據(jù)源配合docker部署mysql主從實(shí)現(xiàn)讀寫分離效果

    springboot多數(shù)據(jù)源配合docker部署mysql主從實(shí)現(xiàn)讀寫分離效果

    這篇文章主要介紹了springboot多數(shù)據(jù)源配合docker部署mysql主從實(shí)現(xiàn)讀寫分離,通過使用docker獲取mysql鏡像,具體內(nèi)容詳情跟隨小編一起看看吧
    2021-09-09
  • 詳解獲取Spring MVC中所有RequestMapping以及對應(yīng)方法和參數(shù)

    詳解獲取Spring MVC中所有RequestMapping以及對應(yīng)方法和參數(shù)

    本篇文章主要介紹了詳解獲取Spring MVC中所有RequestMapping以及對應(yīng)方法和參數(shù),具有一定的參考價值,感興趣的小伙伴們可以參考一下。
    2017-03-03
  • SpringBoot整合OpenCV的實(shí)現(xiàn)示例

    SpringBoot整合OpenCV的實(shí)現(xiàn)示例

    這篇文章主要介紹了SpringBoot整合OpenCV的實(shí)現(xiàn)示例。文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • JAVA8發(fā)送帶有Body的HTTP GET請求

    JAVA8發(fā)送帶有Body的HTTP GET請求

    本文主要介紹了JAVA8發(fā)送帶有Body的HTTP GET請求,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • 簡單探索 Java 中的惰性計(jì)算

    簡單探索 Java 中的惰性計(jì)算

    這篇文章主要介紹了簡單探索 Java 中的惰性計(jì)算,惰性計(jì)算(盡可能延遲表達(dá)式求值)是許多函數(shù)式編程語言的特性。惰性集合在需要時提供其元素,無需預(yù)先計(jì)算它們,這帶來了一些好處。,需要的朋友可以參考下
    2019-06-06
  • SpringBoot實(shí)現(xiàn)輕量級動態(tài)定時任務(wù)管控及組件化的操作步驟

    SpringBoot實(shí)現(xiàn)輕量級動態(tài)定時任務(wù)管控及組件化的操作步驟

    文章介紹了一種在SpringBoot中實(shí)現(xiàn)動態(tài)定時任務(wù)的解決方案,基于COLA架構(gòu)理論,封裝到了組件層,該組件支持類級別和方法級別的定時任務(wù)注冊,并提供了易用性和擴(kuò)展性,組件使用Maven形式引入,并且可以通過YAML配置文件進(jìn)行設(shè)置,感興趣的朋友一起看看吧
    2024-11-11
  • javaweb登錄驗(yàn)證碼的實(shí)現(xiàn)方法

    javaweb登錄驗(yàn)證碼的實(shí)現(xiàn)方法

    這篇文章主要為大家詳細(xì)介紹了javaweb登錄驗(yàn)證碼的實(shí)現(xiàn)方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-11-11

最新評論

灌云县| 呼玛县| 偃师市| 昌图县| 临江市| 鄂伦春自治旗| 柳江县| 从江县| 萝北县| 宁都县| 梅州市| 仁寿县| 丽水市| 沙河市| 南川市| 从江县| 云南省| 曲水县| 竹山县| 阳泉市| 平远县| 开平市| 阜城县| 佛冈县| 潜江市| 阿克陶县| 英山县| 时尚| 广水市| 九江市| 汽车| 天峻县| 襄垣县| 若羌县| 双城市| 德清县| 苍南县| 安陆市| 宜阳县| 贺兰县| 昌黎县|