Java如何在Spring中连接/注入null值?
要将null值连接或注入到bean中,我们可以使用<null>元素。下面的配置向您展示了如何在Spring配置文件中进行配置。null例如,当您要覆盖自动连线到Bean中的值时,可能需要连线一个值。
正如您在下面的配置中看到的那样,我们将Bean中的属性writer的值设置song为A,null并且还将两个null值设置到Bean中的songs属性中album。
<?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="song">
<property name="title" value="I Saw Her Standing There"/>
<property name="writer">
<null/>
</property>
</bean>
<bean id="album">
<property name="title" value="Please Please Me"/>
<property name="year" value="1963"/>
<property name="songs">
<list>
<ref bean="song"/>
<null/>
<null/>
</list>
</property>
</bean>
</beans>