在xml中自动装配不适用于原始数据类型

时间:2015-06-03 15:07:17

标签: java xml spring

我正在尝试这样,而不是在我的bean类中使用注释。我想在类型中使用autowire在string bean中设置值。但价值正如Null一样。当我使用自定义对象(不是原始数据类型)时,我得到的值是我放在引用的bean中。对此有什么看法吗?

Bean.xml:

<?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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

<bean id="salesorder" class="com.demo.bean.salesorder" autowire="byType">
<property name="orderID" value="100010"></property>
</bean>

<bean id="orderdesc" class="java.lang.String">
<constructor-arg value="This is my first reference use in spring"></constructor-arg>
</bean>

</beans>

1 个答案:

答案 0 :(得分:0)

我有理由相信你不能这样做。我认为你只能注入真实的物体,而不是有点像人造物体的物体。示例:String,Long等。

我建议如果你真的想这样做,你可以使用util:properties构造。它不完全相同,但我认为你可以得到你想要的东西。

util:roperties example

或者可能是地图? 例如:

<util:map id="filtersByFlag">
   <entry key="${flag.even}" value-ref="filtersForEven" />
   <entry key="${flag.odd}" value-ref="filtersForOdd" />
</util:map>