SpringBoot获得application.properties中数据的几种方式
本文内容纲要:
-转:https://blog.csdn.net/qq_27298687/article/details/79033102
-SpringBoot获得application.properties中数据的几种方式
-
-第一种方式
-第二种方式(自动装配到Bean中)
-第三种方式(使用@value注解)
转:https://blog.csdn.net/qq_27298687/article/details/79033102
SpringBoot获得application.properties中数据的几种方式
第一种方式
[html]viewplaincopy
- @SpringBootApplication
- publicclassSpringBoot01Application{
- publicstaticvoidmain(String[]args){
- ConfigurableApplicationContextcontext=SpringApplication.run(SpringBoot01Application.class,args);
- <spanstyle="color:#FF0000;">Stringstr1=context.getEnvironment().getProperty("aaa");
- System.out.println(str1);
- }
- }
第二种方式(自动装配到Bean中)
[html]viewplaincopy
- importorg.springframework.beans.factory.annotation.Autowired;
- importorg.springframework.beans.factory.annotation.Value;
- importorg.springframework.core.env.Environment;
- importorg.springframework.stereotype.Component;
- @Component
- publicclassStudent{
- @Autowired
- privateEnvironmentenv;
- publicvoidspeak(){
- System.out.println("=========>"+env.getProperty("aaa"));
- }
- }
第三种方式(使用@value注解)
[html]viewplaincopy
-
packagecom.example.demo.entity;
importorg.springframework.beans.factory.annotation.Value;
importorg.springframework.context.annotation.PropertySource;
importorg.springframework.stereotype.Component;
@Component
@PropertySource("classpath:jdbc.properties")//如果是application.properties,就不用写@PropertySource("application.properties"),其他名字用些
publicclassJdbc{
@Value("${jdbc.user}")
privateStringuser;
@Value("${jdbc.password}")
privateStringpassword;
publicvoidspeack(){
System.out.println("username:"+user+"------"+"password:"+password);
}
}
本文内容总结:转:https://blog.csdn.net/qq_27298687/article/details/79033102,SpringBoot获得application.properties中数据的几种方式,,第一种方式,第二种方式(自动装配到Bean中),第三种方式(使用@value注解),
原文链接:https://www.cnblogs.com/aknife/p/11905920.html热门推荐
10 读书祝福语女生短句简短
11 感谢同事新婚祝福语简短
12 感谢教官文案祝福语简短
13 员工聚会蛋糕祝福语简短
14 元宵发客户祝福语简短
15 给团队信心祝福语简短
16 医美的祝福语大全简短
17 异国姻缘的祝福语简短
18 兄妹拍照祝福语简短的话