一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務器之家 - 編程語言 - JAVA教程 - Springboot讀取配置文件及自定義配置文件的方法

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

2021-03-04 09:50Java從入門到跑路 JAVA教程

這篇文章主要介紹了Springboot讀取配置文件及自定義配置文件的方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下

1.創建maven工程,在pom文件中添加依賴

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<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.創建項目啟動類 startapplication.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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.打開瀏覽器,輸入 即可看到結果

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

6.使用java bean的方式讀取自定義配置文件 define.properties

  defineentity.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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.打開瀏覽器,訪問 ,可以看到輸出結果

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

補充:我的項目的目錄結構

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

總結

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

原文鏈接:http://www.cnblogs.com/kellyJAVA/p/8030395.html?utm_source=tuicool&utm_medium=referral

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产欧美一区二区三区免费看 | 无人区在线观看免费完整版免费 | 精品国产福利片在线观看 | 男人天堂色男人 | 大叔在线观看 | 肉大捧一进一出视频免费播放 | 成人一级黄色大片 | 精品一久久香蕉国产二月 | 国产一区二 | 国产原创精品 | 成年人免费观看视频网站 | 天天摸天天操天天爽 | fulao在线观看的 | 免费在线视频一区 | 免费高清视频免费观看 | 国产嘿咻 | 免费国产好深啊好涨好硬视频 | 青青草国产精品久久碰 | 日本欧美强乱视频在线 | 五月天婷婷亚洲 | 美女福利视频午夜在线 | 99久热只有精品视频免费观看17 | 欧美女人p | 亚洲成年www | 9久re在线观看视频精品 | 99在线精品视频 | 色综合视频一区二区观看 | 日韩在线一区二区三区免费视频 | 日本一在线中文字幕天堂 | japanese人妖xvideos| 香蕉久久夜色精品国产尤物 | 天天操天天爽天天射 | 刺客女仆| 性欧美sexovideotv | 欧美一级欧美三级 | 青草久久网 | 欧美男人的天堂 | 人妖巨茎video | 草莓视频旧版 | 欧美国产影院 | 日本加勒比在线精品视频 |