spring Properties配置文件的读取

  • 1.PropertiesFactoryBean的使用
1.1 spring配置加入如下:
	<bean id="pf" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
		<!--设置properties的路径-->
		<property name="locations">
			<list>
				<value>classpath:server.properties</value>
			</list>
		</property>
	</bean>
1.2 使用xml形式读取
	<bean id="user" class="com.ws.model.User">
		<!--value值格式: PropertiesFactoryBean bena的id + "." + properties的key-->
		<property name="age" value="#{pf.age}"></property>
		<property name="name" value="#{pf.name}"></property>
	</bean>
1.3 使用注解 @Value
	格式:   @Value(value="xx.xx")  : value值格式: PropertiesFactoryBean bena的id + "." + properties的key
			@Value(value = "#{pf.name}")
			private String name;
  • sping配置:
<?xml version="1.0" encoding="UTF-8"?>
<!-- 到入xml文件的约束 -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
             http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
             http://www.springframework.org/schema/context
             http://www.springframework.org/schema/context/spring-context-4.1.xsd
              ">

    <context:component-scan
            base-package="com.ws"></context:component-scan>

    <!--
     classpath : 只会到你的class路径中查找找文件.(推荐使用)
     classpath*:不仅包含class路径,还包括jar文件中(class路径)进行查找.
     注意:用classpath*:需要遍历所有的classpath,所以加载速度是很慢的;因此,在规划的时候,应该尽可能规划好资源文件所在的路径,尽量避免使用classpath*.
     -->
    <bean id="pf" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="locations">
            <list>
                <value>classpath:server.properties</value>
            </list>
        </property>
    </bean>

    <bean id="user" class="com.ws.model.User">
        <property name="age" value="#{pf.age}"></property>
        <property name="name" value="#{pf.name}"></property>
    </bean>

</beans>
	
  • 2.PropertyPlaceholderConfigurer 使用
 2.1 配置
	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:server.properties</value>
            </list>
        </property>
    </bean>
2.2 xml使用
	<!--通过 ${key} 读取properties文件的值 key:properties的key-->
	<bean id="user" class="com.ws.model.User">
        <property name="age" value="${age}"></property>
        <property name="name" value="${name}"></property>
    </bean>
2.3 注解使用
 	@Value(value = "${name}")
 	private String name;
  • 3.全注解方式
	3.1 spring导入标签
	    <!---使用此标签,无需配置PropertyPlaceholderConfigurer 或 PropertiesFactoryBean-->
		<context:property-placeholder location="classpath:server.properties,config.properties"/>
		
		<!--若文件太多,则可以参考如下:-->
		<context:property-placeholder location="classpath:*.properties"/>
		
	3.2 xml读取及注解使用参考 2 ;
  • 4.使用PropertySource注解
	4.1 使用该 注解可以免去xml中配置注解扫描器<context:property-placeholder location="classpath:*.properties"/>
	4.2使用如下
		@Component
		@PropertySource({"classpath:server.properties","classpath:config.properties"})
		//@PropertySource("classpath:server.properties")  扫描单个配置
		public class User implements InitializingBean {}
  • 5.若配置文件找到对应属性,使用默认值,如下:
		//如果没有找到name对应的值,则使用默认值Hello;
		@Value(value="${name:Hello}")  
		private String name;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring框架提供了一种便捷的方式来读取properties文件中的配置信息。在Spring中,我们可以使用PropertyPlaceholderConfigurer或PropertySourcesPlaceholderConfigurer来加载properties文件。 PropertyPlaceholderConfigurer是在Spring 3.1之前版本中使用的类,用于从classpath或指定位置加载properties文件。它可以将properties文件中的键值对替换为Spring配置文件中的占位符。通过将PropertyPlaceholderConfigurer配置为Bean的一部分,我们可以在XML配置文件中使用占位符来引用properties文件中的值。例如,我们可以在XML中配置一个DataSource Bean,并通过占位符引用properties文件中的数据库连接信息。 PropertySourcesPlaceholderConfigurer是从Spring 3.1版本引入的新类,它可以更方便地处理properties文件。与PropertyPlaceholderConfigurer不同,PropertySourcesPlaceholderConfigurer允许我们使用@Value注解来直接注入properties文件中的属性。我们可以在Java配置类或XML配置文件中配置PropertySourcesPlaceholderConfigurer,并在需要的地方使用@Value注解来获取properties文件中的属性值。 无论我们使用哪种方式来读取properties文件,我们都需要确保将其配置为Spring容器的一部分,并以适当的方式加载它。通常,我们会在Spring配置文件中添加一个bean来加载properties文件,并将其配置为其他bean的属性或参数的值。 总结起来,Spring提供了两种主要的方法来读取properties文件:使用PropertyPlaceholderConfigurer或PropertySourcesPlaceholderConfigurer。这些类可以让我们轻松地将properties文件中的配置值引入到Spring容器中,并在我们的应用程序中使用它们。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值