激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|JavaScript|易語(yǔ)言|

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

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

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

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

1.創(chuàng)建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>
  <!-- 單元測(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

?
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 //自動(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.打開瀏覽器,輸入 即可看到結(jié)果

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.打開瀏覽器,訪問(wèn) ,可以看到輸出結(jié)果

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

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

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

總結(jié)

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

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美一级黄色录相 | 久久久无码精品亚洲日韩按摩 | 欧美日韩亚洲在线 | 国产美女自拍av | 国产精品99免费视频 | 午夜视频大全 | 久久久久久精 | 999精品国产 | 一级观看免费完整版视频 | 中文字幕在线亚洲精品 | 欧美成人h版在线观看 | av日韩在线免费观看 | 中文字幕国产一区 | 亚洲成人午夜精品 | 最新av在线播放 | 亚洲福利视频52 | 亚洲午夜一区二区三区 | 欧美综合日韩 | 欧美成人高清在线 | 美女啪网站 | 欧美a欧美| 国产精品自拍99 | 国产高潮失禁喷水爽到抽搐视频 | 久久久国产精品网站 | 欧美毛片| 久久蜜桃精品一区二区三区综合网 | 国产91影院| 91美女啪啪 | 91久久国产露脸精品国产 | 性欧美日本| 国产亚洲精品成人a | 2021免费日韩视频网 | 日本a级一区 | 最新亚洲国产 | 久久久成人免费视频 | 中文字幕免费播放 | 亚洲第一成网站 | 国产精品剧情一区二区在线观看 | 久久出精品| 日本精品免费观看 | 亚洲一区二区欧美 |