Java Spring属性配置程序不工作
我遇到了以下问题:PropertyConfigure中的注入属性似乎不起作用 我得到的错误是Java Spring属性配置程序不工作,java,spring,Java,Spring,我遇到了以下问题:PropertyConfigure中的注入属性似乎不起作用 我得到的错误是 Caused by: java.lang.NumberFormatException: For input string: "${db.maxactive}" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48) at java.lang.Integer.parseInt(Inte
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)
它试图从stacktrace将值“${db.maxactive}”注入dbcp驱动程序。
如果我打开日志,我可以看到以下内容(当我不注入该属性时,这是stacktrace)
***在加载jdbc.properties之前,将在此处引发异常。
[INFO]正在从类路径资源[jdbc.properties]加载属性文件
这让我觉得PropertyConfigure是在尝试注入值之后加载的,因此它注入的是参数名,而不是参数值
<?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>
java类
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:/applicationContext-StandAlone.xml");
有人能给我一些建议吗,这是因为我是从一个独立的应用程序来做的吗?我在一个web应用程序中多次这样做,propertyConfigurer运行良好
注:弹簧3.1.2.1释放
<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>
org.springframework
spring上下文
org.springframework
spring上下文支持
org.springframework
德克萨斯州春季
org.springframework
SpringJMS
org.springframework
春豆
org.springframework
弹簧网
以下代码对我有效。看看它是否对你有帮助
<!-- Remove id of bean -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<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}</value></property>
</bean>
类路径:/jdbc.properties
${db.driver}
我认为查找jdbc.properties
文件可能有问题。
它会抱怨${db.maxactive}
,但如果您硬编码此值,它可能会开始抱怨下一个值
尝试在属性文件位置定义中使用classpath*:
:
<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>
classpath*:jdbc.properties
您可以找到有关在资源路径中使用通配符的更多信息。我以前在开发的应用程序中见过这一点;我发现问题与mybatis有关,SqlSessionFactoryBean也有问题。我没有挖掘太深,但如果你想研究更多,从这个开始-可能是类似的东西,即使你没有使用mybatis 我可以看到我的bean被实例化,并且在从文件加载属性之前设置了它们的属性 我没有使用Spring PropertyPlaceHolderConfigure,而是不得不使用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>
....
元素的属性必须是valuenot ref
我用同样的问题来解决它就是这样!我想回来用更多的细节更新我的问题。我知道我漏掉了关于ibatis的部分,但是当我写它的时候,我不知道这是原因!在我的例子中,它似乎是org.mybatis.spring.mapper.mapperscannerconfiguer(正如JonW提供的链接所暗示的)。我花了一段时间才找到这个话题,但谢谢你指出问题所在!
<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>
<?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>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--basePackage指定要扫描的包,在此包之下的映射器都会被搜索到。
可指定多个包,包与包之间用逗号或分号分隔-->
<property name="basePackage" value="com.hebeu.persist" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>