Spring Boot中配置文件application.properties使用

2025-05-29 0 26

一、配置文档配置项的调用

Spring Boot中配置文件application.properties使用

Spring Boot中配置文件application.properties使用

启动后在浏览器直接输入http://localhost:18080/user/test,就直接打印出配置文件中的配置内容。

二、绑定对象bean调用

有时候属性太多了,一个个绑定到属性字段上太累,官方提倡绑定一个对象的bean,这里我们建一个configbean.java类,顶部需要使用注解@configurationproperties(prefix = “com”)来指明使用哪个

?

1

2

3

4

5

6

7
@configurationproperties(prefix = "com")

public class configbean {

private string name;

private string id;

// 省略getter和setter

}

这里配置完还需要在spring boot入口类加上@enableconfigurationproperties并指明要加载哪个bean,如果不写configbean.class,在bean类那边添加

?

1

2

3

4

5

6

7
@springbootapplication

@enableconfigurationproperties({configbean.class})

public class chapter2application {

public static void main(string[] args) {

springapplication.run(chapter2application.class, args);

}

}

最后在controller中引入configbean使用即可,如下:

?

1

2

3

4

5

6

7

8

9

10
@restcontroller

public class usercontroller {

@autowired

configbean configbean;

@requestmapping("/")

public string hexo(){

return configbean.getname()+configbean.getid();

}

}

三、参数间引用

在application.properties中的各个参数之间也可以直接引用来使用,就像下面的设置:

?

1

2

3
com.name="张三"

com.id="8"

com.psrinfo=${com.name}编号为${com.id}

这样我们就可以只是用psrinfo这个属性就好

四、使用自定义新建的配置文件

  我们新建一个bean类,如下:

?

1

2

3

4

5

6

7

8
@configuration

@configurationproperties(prefix = "com.md")

@propertysource("classpath:test.properties")

public class configtestbean {

private string name;

private string want;

// 省略getter和setter

}

主要就是加了一个注解:@propertysource("classpath:test.properties")

五、配置文件优先级

application.properties和application.yml文件可以放在一下四个位置:

  • 外置,在相对于应用程序运行目录的/congfig子目录里。
  • 外置,在应用程序运行的目录里
  • 内置,在config包内
  • 内置,在classpath根目录

同样,这个列表按照优先级排序,也就是说,src/main/resources/config下application.properties覆盖src/main/resources下application.properties中相同的属性,如图:

此外,如果你在相同优先级位置同时有application.properties和application.yml,那么application.yml里面的属性就会覆盖application.properties里的属性。

ps:下面看下springboot读取application.properties文件

springboot读取application.properties文件,通常有3种方式

1. @value 例如:

?

1

2
@value("${spring.profiles.active}")

private string profileactive;------相当于把properties文件中的spring.profiles.active注入到变量profileactive中

2. @configurationproperties 例如:

?

1

2

3

4

5

6
@component

@configurationproperties(locations = "classpath:application.properties",prefix="test")

public class testproperties {

string url;

string key;

}

其他类中使用时,就可以直接注入该testproperties 进行访问相关的值

3. 使用enviroment 例如:

?

1

2
private enviroment env;

env.getproperty("test.url");

而env方式效率较低

注:@configurationproperties也可用于其他.properties文件,只要locations指定即可

总结

以上所述是小编给大家介绍的spring boot中配置文件application.properties使用,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对快网idc网站的支持!

原文链接:https://www.cnblogs.com/gczr/p/6692054.html

收藏 (0) 打赏

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

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

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

快网idc优惠网 建站教程 Spring Boot中配置文件application.properties使用 https://www.kuaiidc.com/113157.html

相关文章

发表评论
暂无评论