我使用带有@Configuration批注的类来配置我的spring应用程序:
@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类中获取我的DataSource配置对象:
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)
它使用了String属性(driver,url),但$ {minIdle}(类型为int)无法解析! 请帮忙。 Thanx提前!
答案 0 :(得分:4)
<强>更新强>
通过更新的信息和您自己的answer,我删除了所有数据源内容,因为我确信您的问题与<context:property-placeholder />
元素有关。
您认为answer的错误消息是我认为是问题的根源:
引起: java.lang.NumberFormatException:For 输入字符串:“$ {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
元素:
引起: java.lang.NumberFormatException:For 输入字符串:“$ {b}”
使用property-placeholder
,它可以像预期的那样工作。
答案 1 :(得分:2)
我上周遇到了类似的错误。我会检查你的弹簧版本。转到3.0.2在Validator类上修复了类似的错误。
我最近还为数组属性注册了一个错误。这似乎是您的问题,但您可能需要通过执行属性注入的spring代码进行调试,以查看它是如何处理您的属性的。在我的例子中,问题出在BeanDefinitionVistor中,其中包含决定如何处理任何给定属性的代码。跟踪这个课程。可能是它决定不使用属性解析器来注入值。错误表明情况就是这样,因为它看起来像是在尝试传递原始字符串,而不是用传递的值替换它。
祝你好运 德里克答案 2 :(得分:1)
我使用的是Spring 3.0.2。而且似乎只能解析String属性。 我将minIdle属性的类型更改为String:
@Value("${minIdle}") private String minIdle;
在我的dataSorce方法中,我将手动String转换为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; }
它工作正常(没有强制转换) - 我的应用程序运行这些设置。如果我尝试使用我的SpringConfiguartion类作为构造函数参数获取ApplicationContext,则会出现错误:
ApplicatonContext: ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfiguration.class); DataSource dataSource = ctx.getBean(DataSource.class);
完整Stacktrace
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/>