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é)果
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.打開瀏覽器,訪問 ,可以看到輸出結(jié)果
補(bǔ)充:我的項(xiàng)目的目錄結(jié)構(gòu)
總結(jié)
以上所述是小編給大家介紹的springboot讀取配置文件及自定義配置文件的方法,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)服務(wù)器之家網(wǎng)站的支持!
原文鏈接:http://www.cnblogs.com/kellyJAVA/p/8030395.html?utm_source=tuicool&utm_medium=referral