Springboot读取配置文件及自定义配置文件的方法

2025-05-29 0 30

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读取配置文件及自定义配置文件的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对快网idc网站的支持!

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

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Springboot读取配置文件及自定义配置文件的方法 https://www.kuaiidc.com/113856.html

相关文章

发表评论
暂无评论