Spring配置文件中引入properties文件时数据类型报错处理

昨天在本地搭一套spring集成redis服务的时候,遇见一个让人很纠结的事情,启动服务后报错,报错如下:

Info: : 2018/11/03 01:04:41 [WARN] : org.springframework.web.context.support.XmlWebApplicationContext  
Exception encountered during context initialization - cancelling refresh attempt 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'poolConfig' defined in class path resource [Redis.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type 'java.lang.String' to required type 'int' for property 'maxTotal'; nested exception is java.lang.NumberFormatException: For input string: "${jedis_pool_max_active}"
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:548)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
    at 

配置文件如下

#-----------------jedis 连接参数设置-----------------
#redis服务器IP
redis_ip=192.168.5.101
#redis 服务器端口号
redis_port=6379
#-----------------jedis 连接池参数设置-----------------
#jedis最大分配对象
jedis_pool_max_active=3000
#jedis最大保存idel状态对象数
jedis_pool_max_idle=1000
#jedis池没有对象返回时,最大等待时间
jedis_pool_max_wait_millis=10000
#jedis调用borrowObject方法时,是否进行有效检查
jedis_pool_test_on_borrow=true
#jedis调用returnObject方法时,是否进行有效检查
jedis_pool_test_on_return=true

redis.xml如下:根据报错信息,我们可以很明确的知道,就是读取配置文件出错了,昨天因为还要忙其他的,就暂时规避了这个报错,直接把数据写在了xml里面。

现在有时间了,就要考虑下这个问题到底是怎么出现的,以前引用properties也没出现过这种情况。之后就开始查找资料,了解到在Spring配置文件中引入properties文件有这么两种写法:

1、直接使用context:property-placeholder>标签,把jdbc.properties文件路径写进去就可以了。如:

 <context:property-placeholder location="classpath:redis.properties"/>

2、配置成bean的形式,如:

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <!--可以配置多个资源文件 -->
        <list>
	    <value>classpath:redis.properties</value>
        </list>
    </property>
</bean>

用法是:${...}

如果我要取url的数据,直接写成这样:${url}

因此把xml改成第一种形式,毕竟简洁,修改后如下:

这时候再启动服务,发现报错变了:

Info: 2018/11/03 01:29:39 [ERROR] : org.springframework.web.context.ContextLoader  
Context initialization failed 
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from relative location [Redis.xml]
Offending resource: class path resource [applicationContext.xml]; nested exception is org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 10 in XML document from class path resource [Redis.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 10; columnNumber: 72; 元素 "context:property-placeholder" 的前缀 "context" 未绑定。

 这里说明,Spring读取xml的时候解析出错了,根本就读取不了 <context: ...  这个标签,也就是说Spring在校验xml的时候未通过校验,说明这个xml的配置文件中缺少了定义,这就涉及到Spring是如何读取xml文件的了,简单说下就是Spring会读取xml中配置的DTD和XSD来验证xml文件的正确性,这个问题后续会给出详细的解读,这里不展开说明,我们继续解决现在的问题。

既然知道了报错原因,那么就可以去查找解决方案了,可以知道是在xml配置文件头上少了配置,这个很容易查找的,就是要在Redis.xml文件的头文件中添加如下:

xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd

 添加后的xml如下:

此时再来启动服务,服务可以正常启动了。

代码已经提交到git,git地址:https://github.com/gavin-lcrs/redistest

后续会继续完善该项目。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值