Spring中Properties的配置方式
对Spring里面的Properties不理解的开发者可能会觉得有点乱,主要是因为配置方式很多种,使用方式也很多种。
本文不是原理分析、源码分析文章,只是希望可以帮助读者更好地理解和使用SpringProperties。
Properties的使用
本文的读者都是使用过Spring的,先来看看Properties是怎么使用的,Spring中常用的有以下几种使用方式:
1.在xml配置文件中使用
即自动替换${}里面的值。
2.通过@Value注入使用
@Value("${javadoop.jdbc.url}") privateStringurl;
3.通过Environment获取
此法有需要注意的地方。并不是所有的配置方式都支持通过Environment接口来获取属性值,亲测只有使用注解@PropertySource的时候可以用,否则会得到null,至于怎么配置,下面马上就会说。
@Autowired privateEnvironmentenv; publicStringgetUrl(){ returnenv.getProperty("javadoop.jdbc.url"); }
如果是SpringBoot的application.properties注册的,那也是可以的。
Properties配置
前面我们说了怎么使用我们配置的Properties,那么该怎么配置呢?Spring提供了很多种配置方式。
1.通过xml配置
下面这个是最常用的配置方式了,很多项目都是这么写的:
2.通过@PropertySource配置
前面的通过xml配置非常常用,但是如果你也有一种要消灭所有xml配置文件的冲动的话,你应该使用以下方式:
@PropertySource("classpath:sys.properties") @Configuration publicclassJavaDoopConfig{ }
注意一点,@PropertySource在这里必须搭配@Configuration来使用,具体不展开说了。
3.PropertyPlaceholderConfigurer
如果读者见过这个,也不必觉得奇怪,在Spring3.1之前,经常就是这么使用的:
classpath:sys.properties
当然,我们也可以用相应的javaconfiguration的版本:
@Bean publicPropertyPlaceholderConfigurerpropertiess(){ PropertyPlaceholderConfigurerppc=newPropertyPlaceholderConfigurer(); Resource[]resources=newClassPathResource[]{newClassPathResource("sys.properties")}; ppc.setLocations(resources); ppc.setIgnoreUnresolvablePlaceholders(true); returnppc; }
4.PropertySourcesPlaceholderConfigurer
到了Spring3.1的时候,引入了PropertySourcesPlaceholderConfigurer,这是一个新的类,注意看和之前的PropertyPlaceholderConfigurer在名字上多了一个Sources,所属的包也不一样,它在Spring-Context包中。
在配置上倒是没有什么区别:
classpath:sys.properties
也来一个javaconfiguration版本吧:
@Bean publicPropertySourcesPlaceholderConfigurerproperties(){ PropertySourcesPlaceholderConfigurerpspc=newPropertySourcesPlaceholderConfigurer(); Resource[]resources=newClassPathResource[]{newClassPathResource("sys.properties")}; pspc.setLocations(resources); pspc.setIgnoreUnresolvablePlaceholders(true); returnpspc; }
SpringBoot相关
SpringBoot真的是好东西,开箱即用的感觉实在是太好了。这里简单介绍下相关的内容。
快速生成一个SpringBoot项目:https://start.spring.io/
application.properties
我们每个项目都默认有一个application.properties文件,这个配置文件不需要像前面说的那样进行注册,SpringBoot会帮我们自动注册。
当然,也许你想换个名字也是可以的,在启动的时候指定你的文件名字就可以了:
java-Dspring.config.location=classpath:sys.properties-jarapp.jar
application-{env}.properties
为了给不同的环境指定不同的配置,我们会用到这个。
比如测试环境和生产环境的数据库连接信息就不一样。
所以,在application.properties的基础上,我们还需要新建application-dev.properties和application-prd.properties,用于配置环境相关的信息,然后启动的时候指定环境。
java-Dspring.profiles.active=prd-jarapp.jar
结果就是,application.properties和application-prd.properties两个文件中的配置都会注册进去,如果有重复的key,application-prd.properties文件中的优先级较高。
@ConfigurationProperties
这个注解是SpringBoot中才有的。
即使大家不使用这个注解,大家也可能会在开源项目中看到这个,这里简单介绍下。
来一个例子直观一些。按照之前说的,在配置文件中填入下面的信息,你可以选择写入application.properties也可以用第一节介绍的方法。
javadoop.database.url=jdbc:mysql: javadoop.database.username=admin javadoop.database.password=admin123456
java文件:
@Configuration @ConfigurationProperties(prefix="javadoop.database") publicclassDataBase{ Stringurl; Stringusername; Stringpassword; //gettersandsetters }
这样,就在Spring的容器中就自动注册了一个类型为DataBase的bean了,而且属性都已经set好了。
在启动过程中动态修改属性值
这个我觉得都不需要太多介绍,用SpringBoot的应该基本上都知道。
属性配置有个覆盖顺序,也就是当出现相同的key的时候,以哪里的值为准。
启动参数>application-{env}.properties>application.properties
启动参数动态设置属性:
java-Djavadoop.database.password=admin4321-jarapp.jar
另外,还可以利用系统环境变量设置属性,还可以指定随机数等等,确实很灵活,不过没什么用,就不介绍了。
总结
读者如果想要更加深入地了解Spring的Properties,需要去理解Spring的Environment接口相关的源码。建议感兴趣的读者去翻翻源代码看看。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。