资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

application.properties怎么在SpringBoot中使用

今天就跟大家聊聊有关application.properties怎么在Spring Boot中使用,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

创新互联是专业的永川网站建设公司,永川接单;提供网站制作、网站设计,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行永川网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

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

application.properties怎么在Spring Boot中使用

application.properties怎么在Spring Boot中使用

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

二、绑定对象bean调用

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

@ConfigurationProperties(prefix = "com")
public class ConfigBean {
  private String name;
  private String id;

  // 省略getter和setter
}

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

@SpringBootApplication
@EnableConfigurationProperties({ConfigBean.class})
public class Chapter2Application {
  public static void main(String[] args) {
    SpringApplication.run(Chapter2Application.class, args);
  }
}

最后在Controller中引入ConfigBean使用即可,如下:

@RestController
public class UserController {
  @Autowired
  ConfigBean configBean;

  @RequestMapping("/")
  public String hexo(){
    return configBean.getName()+configBean.getId();
  }
}

三、参数间引用 

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

com.name="张三"
com.id="8"
com.psrInfo=${com.name}编号为${com.id}

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

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

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

@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怎么在Spring Boot中使用

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

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

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

1. @Value  例如:

@Value("${spring.profiles.active}")
private String profileActive;------相当于把properties文件中的spring.profiles.active注入到变量profileActive中

2. @ConfigurationProperties  例如:

@Component
@ConfigurationProperties(locations = "classpath:application.properties",prefix="test")
public class TestProperties {
String url;
String key;
}

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

3. 使用Enviroment   例如:

private Enviroment env;
env.getProperty("test.url");

而env方式效率较低

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

看完上述内容,你们对application.properties怎么在Spring Boot中使用有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。


本文标题:application.properties怎么在SpringBoot中使用
文章地址:http://cdkjz.cn/article/jpsseh.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220