spring配置中的context:property-placeholder

论坛 期权论坛 脚本     
匿名技术用户   2020-12-27 10:58   19   0

spring配置中的<context:property-placeholder />

在spring整合redis的过程中,使用
<context:property-placeholder location="classpath:redis.properties" /> 读取配置文件,用以下代码配置redis

<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxTotal" value="${redis.pool.maxTotal}"/>
        <property name="maxIdle" value="${redis.pool.maxIdle}"/>
        <property name="maxWaitMillis" value="${redis.pool.maxWaitMillis}"/>
        <property name="testOnBorrow" value="${redis.pool.testOnBorrow}"/>
    </bean>

但是在项目启动后${redis.pool.maxTotal}等属性值读取不到。

网上资源表示:一个spring项目中只会加载一次property-placeholder,说明我的代码中有其他地方也在使用,同样读取属性文件的地方在配置dataSource的地方。但是dataSource并没有使用<context:property-placeholder />,而是

<bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
        <property name="location" value="classpath:jdbc.properties" />
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
    </bean>

原来这两种是等价的写法,既然等价那么加载jdbc.properties我也换成了<context:property-placeholder location="classpath:jdbc.properties" />
那么这样有两个property-placeholder了也复现了之前的问题,那么对于这两个我都必须使用,网上说可以使用ignore-unresolvable=true来解决,便尝试两个都改成

 <context:property-placeholder location="classpath:redis.properties" ignore-unresolvable="true"/>
  <context:property-placeholder location="classpath:jdbc.properties" ignore-unresolvable="true"/>

但这种情况下出现了jdbc属性读取不到现象

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"  init-method="init"
        destroy-method="close">  
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />

这里读取不到jdbc.driver,jdbc.url等,然后没办法之际将dataSource的配置改成最初的那种并使用ignore-unresolvable=true

 <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
        <property name="location" value="classpath:jdbc.properties" />
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
    </bean>

这个时候问题便解决了。虽然问题解决了但是心里很疑惑,如果它们真的是等价的,为什么jdbc.properties和redis.properties同时使用<context:property-placeholder location="classpath:*.properties" ignore-unresolvable="true"/>加载时只有redis可以加载到属性,而jdbc使用bean,redis使用这种便可同时加载?我觉得它们之间必有什么不同的地方,希望有懂得朋友帮我解惑。

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP