详解Spring Boot读取配置文件与配置文件优先级
SpringBoot读取配置文件
1)通过注入ApplicationContext或者Environment对象来读取配置文件里的配置信息。
packagecom.ivan.config.controller; importorg.springframework.beans.factory.annotation.Autowired; importorg.springframework.context.ApplicationContext; importorg.springframework.core.env.Environment; importorg.springframework.web.bind.annotation.RequestMapping; importorg.springframework.web.bind.annotation.RequestMethod; importorg.springframework.web.bind.annotation.RestController; @RestController publicclassConfigController{ @Autowired ApplicationContextcontext; @Autowired Environmentenvironment; @RequestMapping(value="/config",method={RequestMethod.GET}) publicStringgetConfigContent(){ Stringname=context.getEnvironment().getProperty("db.user.name"); returnname; } @RequestMapping(value="/configEnv",method={RequestMethod.GET}) publicStringgetConfigEnvironment(){ Stringname=environment.getProperty("db.user.name"); returnname; } }
2)通过@ConfigurationProperties配合@PropertySource读取配置文件里的配置信息。
1:通过@PropertySource指定当前类里属性的配置文件地址,ConfigurationProperties可以指定配置的前缀,@Configuration用于定义一个配置类:
packagecom.ivan.config.entity; importorg.springframework.boot.context.properties.ConfigurationProperties; importorg.springframework.context.annotation.Configuration; importorg.springframework.context.annotation.PropertySource; @Configuration @PropertySource("classpath:config/druid.properties") @ConfigurationProperties(prefix="druid") publicclassDruidConfig{ privateintinitialSize; privateintminIdle; privateintmaxActive; privateintmaxWait; privateStringvalidationQuery; privatebooleantestWhileIdle; privatebooleantestOnBorrow; privatebooleantestOnReturn; publicintgetInitialSize(){ returninitialSize; } publicvoidsetInitialSize(intinitialSize){ this.initialSize=initialSize; } publicintgetMinIdle(){ returnminIdle; } publicvoidsetMinIdle(intminIdle){ this.minIdle=minIdle; } publicintgetMaxActive(){ returnmaxActive; } publicvoidsetMaxActive(intmaxActive){ this.maxActive=maxActive; } publicintgetMaxWait(){ returnmaxWait; } publicvoidsetMaxWait(intmaxWait){ this.maxWait=maxWait; } publicStringgetValidationQuery(){ returnvalidationQuery; } publicvoidsetValidationQuery(StringvalidationQuery){ this.validationQuery=validationQuery; } publicbooleanisTestWhileIdle(){ returntestWhileIdle; } publicvoidsetTestWhileIdle(booleantestWhileIdle){ this.testWhileIdle=testWhileIdle; } publicbooleanisTestOnBorrow(){ returntestOnBorrow; } publicvoidsetTestOnBorrow(booleantestOnBorrow){ this.testOnBorrow=testOnBorrow; } publicbooleanisTestOnReturn(){ returntestOnReturn; } publicvoidsetTestOnReturn(booleantestOnReturn){ this.testOnReturn=testOnReturn; } @Override publicStringtoString(){ return"DruidConfig[initialSize="+initialSize+",minIdle="+minIdle+",maxActive="+maxActive+",maxWait="+maxWait+",validationQuery="+validationQuery+",testWhileIdle="+testWhileIdle+",testOnBorrow="+testOnBorrow+",testOnReturn="+testOnReturn+"]"; } }
2:对应的配置文件:
druid.initialSize=5 druid.minIdle=5 druid.maxActive=20 druid.maxWait=60000 druid.validationQuery=select'x' druid.testWhileIdle=true druid.testOnBorrow=true druid.testOnReturn=true
3:在需要用到的类通过@Autowired注入
packagecom.ivan.config.controller; importorg.springframework.beans.factory.annotation.Autowired; importorg.springframework.web.bind.annotation.RequestMapping; importorg.springframework.web.bind.annotation.RequestMethod; importorg.springframework.web.bind.annotation.RestController; importcom.ivan.config.entity.DruidConfig; @RestController publicclassDruidConfigController{ @Autowired publicDruidConfigdruidConfig; @RequestMapping(value="/druidConfig",method={RequestMethod.GET}) publicStringgetDruidConfig(){ returndruidConfig.toString(); } }
3)通过@Value注解
1:需要得到配置属性的类如下,可以在任何需要得到配置的地方用@Value注解
packagecom.ivan.config.entity; importorg.springframework.beans.factory.annotation.Value; importorg.springframework.context.annotation.Configuration; @Configuration publicclassValueTest{ @Value("${db.user.name}") privateStringusername; publicStringgetUsername(){ returnusername; } publicvoidsetUsername(Stringusername){ this.username=username; } }
2:测试Controller类通过@Autowired注入实体类
packagecom.ivan.config.controller; importorg.springframework.beans.factory.annotation.Autowired; importorg.springframework.web.bind.annotation.RequestMapping; importorg.springframework.web.bind.annotation.RequestMethod; importorg.springframework.web.bind.annotation.RestController; importcom.ivan.config.entity.ValueTest; @RestController publicclassValueController{ @Autowired privateValueTestvalue; @RequestMapping(value="/configValue",method={RequestMethod.GET}) publicStringgetConfig(){ returnvalue.getUsername(); } }
Springboot配置文件优先级:
1:命令行参数。(以--开头的参数,比如可以设置:--server.port对同一套代码设置不同的参数)
2:通过System.getProperties()获取的Java系统参数。
3:操作系统环境变量(这解释了为什么你通过application.properties设置的user.name取的是系统的用户名了)
4:从java:comp/env得到的JNDI属性。
5:应用Jar文件之外的属性文件(系统的application.properties文件)
6:应用Jar文件内部的属性文件。
7:在应用配置Java类(包含“@Configuration”注解的Java类)中通过“@PropertySource”注解声明的属性文件。
8:通过“SpringApplication.setDefaultProperties”声明的默认属性。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。