2010-05-16 35 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