1

i am using properties file to offer configuration of database to c3p0 datasource. but i just cannot set correct properties to datasource. my xml file of spring context is like this:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

<mvc:annotation-driven/>

<context:component-scan base-package="com.PitaYa.GuGuDa.controllers"/>

<context:component-scan base-package="com.PitaYa.GuGuDa.service.circle"/>

<bean id="daoScanPackage" class="java.lang.String">
    <constructor-arg type="java.lang.String" value="
        com.PitaYa.GuGuDa.dao.circle
    "/>
</bean>

<!-- <bean id="databaseConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:jdbc.properties"/>
</bean> -->
<context:property-placeholder location="classpath:jdbc.properties" />

<bean id="GuGuDaDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <property name="driverClass" value="com.mysql.jdbc.Driver"/>
    <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/db"/>
    <property name="user" value="user"/>
    <property name="password" value="password"/>
    <property name="maxIdleTime" value="${jdbc.maxIdleTime}"/>
</bean>

<bean id="GuGuDaSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="configLocation" value="classpath:guguda-mybatis-config.xml"/>
    <property name="dataSource" ref="GuGuDaDataSource"/>
</bean>

<bean id="GuGuDaMapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" ref="daoScanPackage"/>
    <property name="sqlSessionFactory" ref="GuGuDaSqlSessionFactory"/>
</bean>

and i put the jdbc.properties file in src/main/resources folder like this:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/GuGuDa
jdbc.username=user
jdbc.password=password
jdbc.initialSize=0
jdbc.maxActive=20
jdbc.maxIdleTime=20
jdbc.minIdleTime=1
jdbc.maxWait=60000

while when i start the tomcat it gets an error:

java.lang.NumberFormatException: For input string: "${jdbc.maxIdleTime}"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:569)
at java.lang.Integer.valueOf(Integer.java:766)

it seems that the datasource is using the configuration literally written in the xml file instead of the actual value in the properties file.

can anyone show me the reason?

*****************update file directory**************** file directory

classpath

*****************update web.xml****************** my web.xml is like this:

<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

**************** update full stack trace ***************

java.lang.NumberFormatException: For input string: "${jdbc.maxIdleTime}"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:569)
at java.lang.Integer.valueOf(Integer.java:766)
at org.springframework.util.NumberUtils.parseNumber(NumberUtils.java:194)
at org.springframework.beans.propertyeditors.CustomNumberEditor.setAsText(CustomNumberEditor.java:113)
at org.springframework.beans.TypeConverterDelegate.doConvertTextValue(TypeConverterDelegate.java:464)
at org.springframework.beans.TypeConverterDelegate.doConvertValue(TypeConverterDelegate.java:437)
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:195)
at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:576)
at org.springframework.beans.AbstractNestablePropertyAccessor.convertForProperty(AbstractNestablePropertyAccessor.java:603)
at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:204)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1527)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1486)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1226)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1481)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1226)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1481)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1226)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:120)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:677)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:519)
at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:667)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:633)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:681)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:552)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:493)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
at javax.servlet.GenericServlet.init(GenericServlet.java:158)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1231)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1144)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1031)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4914)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5201)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Boli-CS
  • 550
  • 1
  • 5
  • 14
  • IMO, this file is not picked up. Could you please check whether the package of this file is included in class path? – achin Dec 11 '15 at 03:14
  • Possible duplicate of http://stackoverflow.com/questions/7191162/spring-propertyplaceholderconfigurer-cannot-find-property-file – achin Dec 11 '15 at 03:17
  • Solved by using "classpath*:jdbc.properties" – achin Dec 11 '15 at 03:18
  • @achin i tried but does not work, too. – Boli-CS Dec 11 '15 at 05:47
  • Could you tell the directory structure where this file is placed? and have you checked that the directory (or the associated jar) is in classpath? – achin Dec 11 '15 at 06:15
  • How are you loading the xml file and please add the full stack trace. – M. Deinum Dec 11 '15 at 07:04
  • @achin i updated as you suggested. – Boli-CS Dec 11 '15 at 07:04
  • @M.Deinum i updated the question. – Boli-CS Dec 11 '15 at 07:06
  • As requested please add the full stack trace not just the snippet you have now. – M. Deinum Dec 11 '15 at 07:16
  • @M.Deinum thanks a lot, i just updated it. – Boli-CS Dec 11 '15 at 08:27
  • 1
    Do you use the datasource in a `BeanPostProcessor` or `BeanFactoryPostProcessor` or anywhere else which triggers early usage of the datasource? – M. Deinum Dec 11 '15 at 09:09
  • @M.Deinum no, there is no class in my project implements these two interfaces. – Boli-CS Dec 11 '15 at 10:33
  • That doesn't matter it doesn't have to be one of your own classes. Can you add your full configuration? And instead the `PropertyPlaceholderConfigurer` I suggest using `' instead. – M. Deinum Dec 11 '15 at 10:38
  • @M.Deinum thanks for your answer. I tried `' before but it does not work and i post the full configuration in the question. – Boli-CS Dec 11 '15 at 10:51
  • 1
    You are using the `DataSource` for the `MapperScannerConfigurer` which itself is a `BeanFactoryPostProcessor`. hence your datasource is being instantiated before the placeholder configurer can do it's magic to replace the placeholders. It simply isn't there yet or hasn't processed the definitions yet. – M. Deinum Dec 12 '15 at 15:32
  • @M.Deinum thanks a lot. I think your answer is right. In the statement of the "org.mybatis.spring.mapper.MapperScannerConfigurer", I changed the initialization of the SqlSessionFactory to this:" " and the properties of database is right now. – Boli-CS Dec 18 '15 at 02:14
  • @M.Deinum But I still have one questions:1. I know that "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" also implements "BeanFactoryPostProcessor", but after I add "" after "" to make PropertyPlaceholderConfigurer have the highest priority, it still does not work, too. PropertyPlaceholderConfigurer is supposed to initialized first, but the error is still there. – Boli-CS Dec 18 '15 at 02:21
  • Not in this case, it doesn't matter how high or low you set the order. The problem is that the `MapperScannerConfigurer` is also a `BeanFactoryPostProcessor` which makes it by default not processable by other `BeanFactoryPostProcessor`s and in turn all the beans it references as those are instantiated early on in the proces. – M. Deinum Dec 18 '15 at 06:56
  • @M.Deinum thanks for your answer. I think I understand the reason of the error. So when I change code to this:" ", why the datasource can get the right properties? – Boli-CS Dec 18 '15 at 11:31
  • Because the init of those beans is delayed upon the time it is actually needed. A `ref` means that it needs to instantiate the beans before hand to be able to inject the dependency. – M. Deinum Dec 18 '15 at 11:53
  • @M.Deinum thanks a lot. you helped me a lot to understand it. – Boli-CS Dec 28 '15 at 11:50

0 Answers0