我遇到了以下问题,propertyConfigurer 的注入属性似乎不起作用。
我得到的错误是......
Caused by: java.lang.NumberFormatException: For input string: "${db.maxactive}"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:449)
at java.lang.Integer.valueOf(Integer.java:554)
at org.springframework.util.NumberUtils.parseNumber(NumberUtils.java:155)
at org.springframework.beans.propertyeditors.CustomNumberEditor.setAsText(CustomNumberEditor.java:115)
at org.springframework.beans.TypeConverterDelegate.doConvertTextValue(TypeConverterDelegate.java:434)
at org.springframework.beans.TypeConverterDelegate.doConvertValue(TypeConverterDelegate.java:406)
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:163)
at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:470)
它试图从堆栈跟踪中将值“${db.maxactive}”注入 dbcp 驱动程序。如果我打开日志记录,我可以看到以下内容(这是我不注入该属性时的堆栈跟踪)......
[INFO] Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@19bd03e: startup date [Thu Jun 27 08:58:08 BST 2013]; root of context hierarchy
[INFO] Loading XML bean definitions from class path resource [conf/spring/applicationContext-StandAlone.xml]
[INFO] Loading XML bean definitions from class path resource [conf/spring/applicationContext-monitoring.xml]
[INFO] Loading XML bean definitions from class path resource [conf/spring/dataAccessContext-local.xml]
[INFO] Loading XML bean definitions from class path resource [conf/spring/applicationContext-jdbc.xml]
[INFO] Loading XML bean definitions from class path resource [conf/spring/applicationContext-beans.xml]
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/Users/porterj/.m2/repository/org/slf4j/slf4j-jdk14/1.6.1/slf4j-jdk14-1.6.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Users/porterj/.m2/repository/org/slf4j/slf4j-nop/1.6.1/slf4j-nop-1.6.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
** * *在加载 jdbc.properties 之前,这里会抛出异常。[INFO] 从类路径资源 [jdbc.properties] 加载属性文件
这让我认为 propertyConfigurer 在尝试注入值之后被加载,因此它注入的是参数名称,而不是参数值。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="false"/>
<property name="locations">
<list>
<value>classpath:/jdbc.properties</value>
</list>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${db.driver}"/>
<property name="url" value="${db.url}"/>
<property name="username" value="${db.username}"/>
<property name="password" value="${db.password}"/>
<property name="maxActive" value="${db.maxactive}"/>
</bean>
然后我的 jdbc.properties 文件...
db.driver=com.ibm.db2.jcc.DB2Driver
db.url=jdbc:db2://dbserver:51000/db
db.username=dm
db.password=pass
db.maxactive=20
java类...
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:/applicationContext-StandAlone.xml");
有人可以给我一些建议,这是因为我是从独立应用程序中执行此操作的吗?我在网络应用程序中做过很多次,并且 propertyConfigurer 工作正常。
注意:Spring 3.1.2.RELEASE
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>