2010-05-16 36 views
5

我使用類@Configuration註釋來配置我的Spring應用程序:問題與Spring @Configuration類

 

@Configuration 
public class SpringConfiguration { 

@Value("${driver}") 
String driver; 

@Value("${url}") 
String url; 

@Value("${minIdle}") 
private int minIdle; 
     // snipp .. 

@Bean(destroyMethod = "close") 
public DataSource dataSource() { 
    DataSource dataSource = new DataSource(); 
    dataSource.setDriverClassName(driver); 
    dataSource.setUrl(url); 
    dataSource.setUsername(user); 
    dataSource.setPassword(password); 
    dataSource.setMinIdle(minIdle); 

    return dataSource; 
} 



和屬性文件在CLASSPATH

 
driver=org.postgresql.Driver 
url=jdbc:postgresql:servicerepodb 
minIdle=1 

我想獲得我的數據源配置的對象在我的DAO類中:

 
ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfiguration.class); 
DataSource dataSource = ctx.getBean(DataSource.class); 

但是我得到的錯誤:

 

org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'springConfiguration': Injection of autowired dependencies 
failed; nested exception is org.springframework.beans.factory.BeanCreationException: 
Could not autowire field: private int de.hska.repo.configuration.SpringConfiguration.minIdle; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'int'; nested exception is **java.lang.NumberFormatException: For input string: "${minIdle}"** 

Caused by: java.lang.NumberFormatException: For input string: **"${minIdle}"** 
at java.lang.NumberFormatException.forInputString(**Unknown Source**) 
at java.lang.Integer.parseInt(Unknown Source) 
at java.lang.Integer.valueOf(Unknown Source) 

它使用字符串屬性(驅動程序,url),但$ {minIdle}(int類型)無法解析! 請幫忙。感謝提前!

回答

2

上週我有類似的錯誤。我會檢查你的春天版本。在3.0.2中,我爲Validator類修復了一個類似的錯誤。

我最近還註冊了一個數組屬性的bug。這似乎是你的問題,但你可能需要通過屬性注入的Spring代碼進行調試,以查看它是如何處理你的財產的。在我的情況下,問題出在BeanDefinitionVistor中,它包含決定如何處理任何給定屬性的代碼。跟蹤這個班級。這可能是它決定不使用屬性解析器來注入值。這個錯誤表明這是事實,因爲它看起來像試圖傳遞原始字符串,而不是用你傳遞的值替換它。

好運 德里克

1

我使用Spring 3.0.2。似乎只有String屬性可以解決。 我改變minIdle屬性的類型爲String:

 
@Value("${minIdle}") 
private String minIdle; 

在我dataSorce方法我轉換手動字符串爲int:

 

    @Bean(destroyMethod = "close") 
    public DataSource dataSource() { 
     DataSource dataSource = new org.apache.tomcat.jdbc.pool.DataSource() 
     dataSource.setDriverClassName(driver); 
     dataSource.setUrl(url); 
     dataSource.setUsername(user); 
     dataSource.setPassword(password); 
       //convert manual 
     dataSource.setMinIdle(Integer.valueOf(minIdle)); 

     return dataSource; 
    } 

它工作正常(無投爲好) - 我的應用程序使用這些運行settings.It涉及到錯誤,如果我試圖用我的SpringConfiguartion類作爲構造函數的參數來獲得的ApplicationContext:

 
ApplicatonContext: ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfiguration.class); 
DataSource dataSource = ctx.getBean(DataSource.class); 

完整堆棧跟蹤

 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in class de.hska.repo.configuration.SpringConfiguration: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.transaction.PlatformTransactionManager de.hska.repo.configuration.SpringConfiguration.transactionManager()] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class de.hska.repo.configuration.SpringConfiguration: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [protected javax.persistence.EntityManagerFactory de.hska.repo.configuration.SpringConfiguration.entityManagerFactory()] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class de.hska.repo.configuration.SpringConfiguration: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [protected javax.sql.DataSource de.hska.repo.configuration.SpringConfiguration.dataSource()] threw exception; nested exception is java.lang.NumberFormatException: For input string: "${minIdle}" 
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:568) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:973) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:879) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:563) 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:872) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:423) 
    at org.springframework.context.annotation.AnnotationConfigApplicationContext.(AnnotationConfigApplicationContext.java:65) 
    at de.hska.repo.repository.CategoryJpaDao.getNumberOfAllRepoObjectsByCategoryId(CategoryJpaDao.java:90) 
    at de.hska.repo.service.CategoryServiceImpl.deleteCategory(CategoryServiceImpl.java:124) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) 
    at org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor.invoke(AfterReturningAdviceInterceptor.java:50) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) 
    at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:50) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) 
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) 
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202) 
    at $Proxy56.deleteCategory(Unknown Source) 
    at de.hska.repo.service.CategoryTest.deleteCategory(CategoryTest.java:465) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) 
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) 
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) 
    at org.junit.rules.ExpectedException$ExpectedExceptionStatement.evaluate(ExpectedException.java:110) 
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74) 
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82) 
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) 
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) 
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) 
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180) 
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46) 
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) 
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.transaction.PlatformTransactionManager de.hska.repo.configuration.SpringConfiguration.transactionManager()] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class de.hska.repo.configuration.SpringConfiguration: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [protected javax.persistence.EntityManagerFactory de.hska.repo.configuration.SpringConfiguration.entityManagerFactory()] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class de.hska.repo.configuration.SpringConfiguration: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [protected javax.sql.DataSource de.hska.repo.configuration.SpringConfiguration.dataSource()] threw exception; nested exception is java.lang.NumberFormatException: For input string: "${minIdle}" 
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:158) 
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:557) 
    ... 60 more 
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class de.hska.repo.configuration.SpringConfiguration: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [protected javax.persistence.EntityManagerFactory de.hska.repo.configuration.SpringConfiguration.entityManagerFactory()] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class de.hska.repo.configuration.SpringConfiguration: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [protected javax.sql.DataSource de.hska.repo.configuration.SpringConfiguration.dataSource()] threw exception; nested exception is java.lang.NumberFormatException: For input string: "${minIdle}" 
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:568) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:973) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:879) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190) 
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:206) 
    at de.hska.repo.configuration.SpringConfiguration$$EnhancerByCGLIB$$9f63c721.entityManagerFactory() 
    at de.hska.repo.configuration.SpringConfiguration.transactionManager(SpringConfiguration.java:154) 
    at de.hska.repo.configuration.SpringConfiguration$$EnhancerByCGLIB$$9f63c721.CGLIB$transactionManager$0() 
    at de.hska.repo.configuration.SpringConfiguration$$EnhancerByCGLIB$$9f63c721$$FastClassByCGLIB$$6e338b8e.invoke() 
    at net.sf.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:215) 
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:210) 
    at de.hska.repo.configuration.SpringConfiguration$$EnhancerByCGLIB$$9f63c721.transactionManager() 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:146) 
    ... 61 more 
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [protected javax.persistence.EntityManagerFactory de.hska.repo.configuration.SpringConfiguration.entityManagerFactory()] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class de.hska.repo.configuration.SpringConfiguration: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [protected javax.sql.DataSource de.hska.repo.configuration.SpringConfiguration.dataSource()] threw exception; nested exception is java.lang.NumberFormatException: For input string: "${minIdle}" 
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:158) 
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:557) 
    ... 82 more 
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class de.hska.repo.configuration.SpringConfiguration: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [protected javax.sql.DataSource de.hska.repo.configuration.SpringConfiguration.dataSource()] threw exception; nested exception is java.lang.NumberFormatException: For input string: "${minIdle}" 
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:568) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:973) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:879) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190) 
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:206) 
    at de.hska.repo.configuration.SpringConfiguration$$EnhancerByCGLIB$$9f63c721.dataSource() 
    at de.hska.repo.configuration.SpringConfiguration.entityManagerFactory(SpringConfiguration.java:127) 
    at de.hska.repo.configuration.SpringConfiguration$$EnhancerByCGLIB$$9f63c721.CGLIB$entityManagerFactory$3() 
    at de.hska.repo.configuration.SpringConfiguration$$EnhancerByCGLIB$$9f63c721$$FastClassByCGLIB$$6e338b8e.invoke() 
    at net.sf.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:215) 
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:210) 
    at de.hska.repo.configuration.SpringConfiguration$$EnhancerByCGLIB$$9f63c721.entityManagerFactory() 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:146) 
    ... 83 more 
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [protected javax.sql.DataSource de.hska.repo.configuration.SpringConfiguration.dataSource()] threw exception; nested exception is java.lang.NumberFormatException: For input string: "${minIdle}" 
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:158) 
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:557) 
    ... 104 more 
Caused by: java.lang.NumberFormatException: For input string: "${minIdle}" 
    at java.lang.NumberFormatException.forInputString(Unknown Source) 
    at java.lang.Integer.parseInt(Unknown Source) 
    at java.lang.Integer.valueOf(Unknown Source) 
    at de.hska.repo.configuration.SpringConfiguration.dataSource(SpringConfiguration.java:109) 
    at de.hska.repo.configuration.SpringConfiguration$$EnhancerByCGLIB$$9f63c721.CGLIB$dataSource$2() 
    at de.hska.repo.configuration.SpringConfiguration$$EnhancerByCGLIB$$9f63c721$$FastClassByCGLIB$$6e338b8e.invoke() 
    at net.sf.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:215) 
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:210) 
    at de.hska.repo.configuration.SpringConfiguration$$EnhancerByCGLIB$$9f63c721.dataSource() 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:146) 
    ... 105 more 


我的XML配置文件:

<context:property-placeholder location="classpath:META-INF/spring.properties"/> 
<context:component-scan base-package="de.hska.repo" /> 

<tx:annotation-driven mode="aspectj"/> 
<context:load-time-weaver/> 

<aop:aspectj-autoproxy/> 

+2

'@ Value'確實可以處理非字符串屬性,並且如果它在這個特定情況下不起作用,那麼奇怪的事情正在發生。 – skaffman 2010-05-16 08:29:39

+0

它可以與字符串屬性一起工作(我的應用程序使用這些設置運行),但不是如果我嘗試獲取ApplicatonContext: ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfiguration.class); – easyrider 2010-05-16 08:47:21

4

更新時間: 隨着更新的信息和自己的answer,我刪除了所有數據源的東西,因爲我相當確信你的問題與<context:property-placeholder />元素有關。

你的錯誤信息,從您的answer,我認爲這是問題的根源:

Caused by: java.lang.NumberFormatException: For input string: "${minIdle}"

我相信問題是PropertyPlaceholderConfigurer未初始化。如果使用<context:property-placeholder />元素對此進行配置,則在初始化過程結束時和Spring容器處於運行階段之前,所有佔位符都將在後處理操作中替換爲來自屬性文件的對應值。

如果它是一個解析錯誤,屬性文件包含這樣一行:

minIdle=demo string 

然後,該錯誤信息會用的"demo string"代替"${minIdle}"

當涉及到解析, Spring將使用@Value註釋處理您的解析並且您的minIdle字段可在我的計算機上正常工作:

@Value("${minIdle}") 
private int minIdle; 

我設法得到您的例外信息的唯一方法是如果我忘記添加 <context:property-placeholder />元素。然後,我得到了與錯誤消息中最後訪問的屬性相同的異常消息。

演示應用

一個小實驗,模擬你的問題:

@Value("${a}") 
private String a; 

@Value("${b}") 
private Integer b; 

@Bean 
public String demo() { 
    System.out.println("a: " + a); 
    System.out.println("b: " + b); 

    return a + ", " + b; 
} 

沒有property-placeholder元素:

Caused by: java.lang.NumberFormatException: For input string: "${b}"

有了它像預期property-placeholder

+0

我使用org.apache.tomcat.jdbc.pool.DataSource – easyrider 2010-05-16 09:20:39

+0

對不起。我沒有看到你的數據源的包名。 – Espen 2010-05-16 09:31:43

+0

@Espen是正確的,我得到了同樣的錯誤與Spring 3.1.2沒有'property-placeholder'在我們的項目屬性正在加載'org.apache.commons.configuration.PropertiesConfiguration' – 2015-06-19 13:49:50