为什么 Spring 会尝试使用属性变量引用字符串而不是值? [英] Why would Spring be trying to use the properties variable reference string instead of the value?

查看:32
本文介绍了为什么 Spring 会尝试使用属性变量引用字符串而不是值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简而言之,问题如下:

<bean id="handlerFactory" class="com.westfieldgrp.audit.jdklogging.cpm.CPMHandlerFactory">
    <property name="schemaName" value="${env.audit.databaseSchema}" />
    <property name="bufferSize" value="${env.audit.bufferSize}" /> 
    <property name="threaded" value="${env.audit.threadedAuditHandler}" />
    <property name="dataSourceSelector" ref="dataSourceSelector" />
</bean>

CPMHandlerFactory 上的 bufferSize 是一个整数.Spring 失败,因为它试图将值设置为 '${env.audit.bufferSize}' 而不是属性文件中的实际值.

bufferSize on the CPMHandlerFactory is an int. Spring is failing because it is trying to set the value to '${env.audit.bufferSize}' instead of the actual value from the properties file.

现在,当我更改属性文件的名称或文件中的 env.audit.bufferSize 时,Spring 会抱怨它找不到属性env.audit.bufferSize".这对我说它可以找到该属性,但不是将值设置为20",而是尝试将其设置为${env.audit.bufferSize}".谁能解释为什么 Spring 可能会这样做以及我可以做些什么?

Now, when I change the name of the properties file or env.audit.bufferSize in the file, Spring complains that it can't find the property 'env.audit.bufferSize'. This says to me that it CAN find the property, but instead of setting the value to '20', it's trying to set it to '${env.audit.bufferSize}'. Can anyone explain why Spring might be doing this and what I can do about it?

以下属性文件的内容:

env.audit.databaseSchema=TDB2DATA
env.audit.dataSourceName=java:comp/env/AuditDataSourceAlias
env.audit.bufferSize=20
env.audit.threadedAuditHandler=true

谢谢,彼得

感谢下面的 Jamestastic 发现了问题.原来是这样:我们有一个看起来像这样的主"上下文文件:

Found the problem thanks to Jamestastic below. Here's what it was: We have a "master" context file that looks like so:

<import resource="environmentBeans.xml" />
<import resource="serviceBeans.xml" />
<import resource="auditBeans.xml" />

'environmentBeans.xml' 中包含 PropertyPlaceholderConfigurer.问题是,我添加了一些引用auditBeans.xml"上下文的代码,当然没有有配置器.我将其切换为引用主"上下文,效果很好.

The 'environmentBeans.xml' has the PropertyPlaceholderConfigurer in it. The problem was, I added some code that referenced the 'auditBeans.xml' context, which of course doesn't have the configurer. I switched it to reference the 'master' context and it works great.

关键是理解为什么这些值不会被替换:因为没有属性配置器.

The key was understanding why the values wouldn't get substituted out: because there was no property configurer.

所以,谢谢!

推荐答案

你记得添加 PropertyPlaceholderConfigurer bean对 Spring 上下文的定义?

Did you remember to add <context:property-placeholder /> or a PropertyPlaceholderConfigurer bean definition to your Spring context?

这篇关于为什么 Spring 会尝试使用属性变量引用字符串而不是值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆