Problem with Spring @Configuration class
i use class with @Configuration annotation to configure my spring application:
@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; }
and properties file in CLASSPATH
driver=org.postgresql.Driver url=jdbc:postgresql:servicerepodb minIdle=1
I would like to get my DataSource configured object in my DAO class:
ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfiguration.class); DataSource dataSource = ctx.getBean(DataSource.class);
But i get the error:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCr开发者_如何学GoeationException: 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)
It worked with String properties (driver, url), but ${minIdle} (of type int) can't be resolved! Please help. Thanx in advance!
Updated:
With the updated information and your own answer, I removed all the datasource stuff, since I'm quite convinced your problem is related to the the <context:property-placeholder />
element.
Your error message from your answer that I believe is the root of the problem:
Caused by: java.lang.NumberFormatException: For input string: "${minIdle}"
I believe the problem is that PropertyPlaceholderConfigurer
isn't initialized. If you configure this with the <context:property-placeholder />
element, all placeholders will be substitued with corresponding values from a properties file in a post process operation at the end of the initialization process and before the Spring container is in the operational phase.
If it was a parse error, and the properties file contained a line like this:
minIdle=demo string
Then, the error message would have used "demo string"
instead of "${minIdle}"
When it comes to parsing, Spring will handle the parsing for you with the @Value
annotation and your minIdle
field works fine on my computer:
@Value("${minIdle}")
private int minIdle;
The only way I managed to get your exception message is if I forgot to add the
<context:property-placeholder />
element. Then I got the same exception message with the last property accessed in the error message.
Demo app
A little experiment that simulates your problem:
@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;
}
Without the property-placeholder
element:
Caused by: java.lang.NumberFormatException: For input string: "${b}"
With the property-placeholder
it works like expected.
I've had some similar errors last week. I'd check which version of spring you are on. Going to 3.0.2 fixed a similar error for me on a Validator class.
I also recently registered a bug for properties that are arrays. This does appear to be your problem, but you may need to debug through the spring code that does the property injection to see how it is handling your property. In my case the problem was in the BeanDefinitionVistor which contains the code which decides how to handle any given property. Trace through this class. It may be that it's deciding not to use the property resolver to inject the value. The error suggests this is the case because it looks like it's trying to pass the raw string, rather than replacing it with your passed value.
Good luck Derek
I use Spring 3.0.2. and it seems only String properties can be resolved. I changed type of minIdle property to String:
@Value("${minIdle}") private String minIdle;
In my dataSorce method i converted manual String to 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; }
It works fine (without cast as well) - my application runs with these settings.It comes to error if i try to get ApplicationContext with my SpringConfiguartion class as constructor parameter:
ApplicatonContext: ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfiguration.class); DataSource dataSource = ctx.getBean(DataSource.class);
Full 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
My xml config file:
<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/>
精彩评论