Member.java
package www.csdn.spring.constructor.bean; public class Member { }
MemberBean.java
package www.csdn.spring.constructor.bean; public class MemberBean { public String name; public Member member; public void setMember(Member member) { this.member = member; } public void setName(String name) { this.name = name; } }
spring-null.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="memberBean" class="www.csdn.spring.constructor.bean.MemberBean"> <!-- value="null" null是字符串 --> <property name="name"> <null /> </property> <property name="member"> <null /> </property> </bean> </beans>
MemberTest.java
package www.csdn.spring.constructor.bean; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MemberTest { @Test public void testBean(){ ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-null.xml"); MemberBean bean = context.getBean("memberBean", MemberBean.class); System.out.println(bean.name); System.out.println(bean.member); } }
控制台輸出:
null
null
它們代表空值