java 如何将包含 < 字符的字符串属性注入 Spring bean?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10171183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to inject string properties containing < character into Spring beans?
提问by Monika Michael
I have following bean definition for my DAO -
我的 DAO 有以下 bean 定义 -
<bean id="userDao" class="UserDao">
<property name="dataSource" ref="dataSource" />
<property name="queryPurgeInvalidReferrals" value="delete from reference where date < ?" />
</bean>
I am getting the error - The value of attribute "value" associated with an element type "property" must not contain the '<' character.
我收到错误 - 与元素类型“属性”关联的属性“值”的值不得包含“<”字符。
回答by axtavt
Since Spring config is an XML file, you need to escape <
according to XML syntax:
由于 Spring config 是一个 XML 文件,所以需要<
根据 XML 语法进行转义:
<property name="queryPurgeInvalidReferrals"
value="delete from reference where date < ?" />
回答by adarshr
Try with this:
试试这个:
<property name="queryPurgeInvalidReferrals">
<value><![CDATA[delete from reference where date < ?]]></value>
</property>
回答by vertti
Use unicode like
使用 unicode 之类的
<property name="queryPurgeInvalidReferrals" value="delete from reference where date < ?" />
回答by Sumit Singh
in your value string <must be replaced by <
在您的值字符串<必须替换为<
value="delete from reference where date < ?"