我使用类@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类型)无法解析! 请帮忙。感谢提前!
'@ Value'确实可以处理非字符串属性,并且如果它在这个特定情况下不起作用,那么奇怪的事情正在发生。 – skaffman 2010-05-16 08:29:39
它可以与字符串属性一起工作(我的应用程序使用这些设置运行),但不是如果我尝试获取ApplicatonContext: ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfiguration.class); – easyrider 2010-05-16 08:47:21