spring项目启动报"Could not resolve placeholder"解决

事出原因

spring项目中 新增了一个xml配置文件,此文件中 引入了 property 文件  替换xml的变量

异常错误如下:

	at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:76)
	at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1309)
	at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1401)
	at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:829)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:361)
	at sun.rmi.transport.Transport$1.run(Transport.java:200)
	at sun.rmi.transport.Transport$1.run(Transport.java:197)
	at java.security.AccessController.doPrivileged(Native Method)
	at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
	at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568)
	at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826)
	at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:683)
	at java.security.AccessController.doPrivileged(Native Method)
	at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'maxIdle' in string value "${maxIdle}"
	at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174)
	at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126)
	at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer$PlaceholderResolvingStringValueResolver.resolveStringValue(PropertyPlaceholderConfigurer.java:259)
	at org.springframework.beans.factory.config.BeanDefinitionVisitor.resolveStringValue(BeanDefinitionVisitor.java:282)

解决思路

因其他的xml文件也有引入property文件的做法,并没有出现问题,怎么新增的这个配置会出事呢。。。

例如 mybatis 的配置  引入 jdbc.properties 就是正常的

    <!-- 引入配置文件 -->
    <bean id="propertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:jdbc.properties"/>
    </bean>

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
        <property name="driverClassName" value="${db_driver}"/>
        <property name="url" value="${db_url}"/>
        <property name="username" value="${db_user_name}"/>
        <property name="password" value="${db_password}"/>
        <!-- 配置监控统计拦截的filters 去掉后监控界面sql无法统计
            开启web监控、开启sql防火墙 -->
        <property name="filters" value="${db_filters}"></property>
        <!-- 初始化连接大小 -->
        <property name="initialSize" value="${db_initialSize}"></property>
        <!-- 连接池最大数量 -->
        <property name="maxActive" value="${db_maxActive}"></property>
        <!-- 连接池最大空闲 -->
        <property name="maxIdle" value="${db_maxIdle}"></property>
        <!-- 连接池最小空闲 -->
        <property name="minIdle" value="${db_minIdle}"></property>

本次新增的是redis的配置 

    <!-- 引入配置文件 -->
    <bean id="redisConfig"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:cache/redis.properties"/>
    </bean>
   
    <!-- 配置自定义缓存管理器,中引入redis缓存管理器 -->
    <bean id = "shiroSpringCacheManager" class="com.jufengad.insurance.api.global.cache.ShiroSpringCacheManager">
        <property name="cacheManager" ref="redisCacheManager"/>
    </bean>


    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxIdle" value="${maxIdle}" />
        <property name="testOnBorrow" value="${testOnBorrow}" />
    </bean>
    <bean id="redisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="${host}" />
        <property name="port" value="${port}" />
        <property name="password" value="${password}" />
        <property name="poolConfig" ref="poolConfig"/>
    </bean>

解决

经研究发现spring扫描注册PropertyPlaceholderConfigurer的bean被设置为单例模式,spring只会扫描一个PropertyPlaceholderConfigurer或者<context:property-placeholder>配置,其它多余将被忽略,所以才会出现替代属性值的占位符无法解析。

因此无论 bean的配置 还是 context的配置,对加入忽略不能解析的设置

修改配置为 context的形式

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

如果还想用之前的bean,那么需要加入属性<property name="ignoreUnresolvablePlaceholders" value="true" />

还有就是集中管理,不在各自的xml中引入properities文件,统一放在一个xml文件中

<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:jdbc.properties</value>
            <value>classpath:cache/redis.properties</value>
        </list>
    </property>
</bean>

到此,正常启动!

发布了59 篇原创文章 · 获赞 11 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/zhangjianming2018/article/details/86649025
今日推荐