gpt4 book ai didi

java - Spring propertyConfigurer 不工作

转载 作者:搜寻专家 更新时间:2023-11-01 02:27:53 26 4
gpt4 key购买 nike

我遇到了以下问题,propertyConfigurer 的注入(inject)属性似乎不起作用。

我得到的错误是...

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}”注入(inject)到 dbcp 驱动程序中。如果我打开日志记录,我可以看到以下内容(这是我不注入(inject)该属性时的堆栈跟踪)...

[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 是在它尝试注入(inject)值之后加载的,因此它注入(inject)的是参数名称,而不是参数值。

<?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");

有人可以给我一些建议吗,这是因为我是通过独立应用程序执行此操作的吗?我已经在 Web 应用程序中多次执行此操作,并且 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>

最佳答案

我以前在我开发的应用程序中见过一次;我发现问题与mybatis有关,并且SqlSessionFactoryBean有问题。我没有深入挖掘,但如果您想进行更多研究,请从这个 link 开始- 即使您不使用 mybatis,也可能是类似的东西。

我可以看到我的 bean 正在被实例化,并且在从文件加载属性之前设置了它们的属性。

我没有使用 Spring PropertyPlaceholderConfigurer,而是不得不使用 spring 的“util:properties”机制来加载属性——一旦我转向这个,它就开始工作了。试试下面的代码 - 可能对你有用......

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">

<util:properties id="dataSourceProps" location="classpath:jdbc.properties"/>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="#{dataSourceProps['db.driver']}" />
<property name="url" value="#{dataSourceProps['db.url']}" />
<property name="username" value="#{dataSourceProps['db.username']}" />
<property name="password" value="#{dataSourceProps['db.password']}" />
<property name="maxActive" value="#{dataSourceProps['db.maxactive']}" />
</bean>

....

</beans>

关于java - Spring propertyConfigurer 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17337826/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com