举例说明:
Cat类
package per.sxhzs.spring6.bean;
public class Cat {
private String name;
private int age;
@Override
public String toString() {
return "Cat{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
}
配置文件set-di.xml
<bean id="catBean" class="per.sxhzs.spring6.bean.Cat">
<!--不给属性注入,属性的默认值就是null-->
<!--<property name="name" value="hxh"/>-->
<!--这不是注入null,这只是注入了一个“null“字符串-->
<!--<property name="name" value="null"/>-->
<!--这种方式是手动注入null-->
<property name="name">
<null/>
</property>
<property name="age" value="20"/>
</bean>
测试类
@Test
public void testNull() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("set-di.xml");
Cat catBean = applicationContext.getBean("catBean", Cat.class);
System.out.println(catBean);
}
标签:set,name,spring,age,Cat,null,public,String From: https://www.cnblogs.com/sxhxh/p/16885569.html运行结果