Spring 注入 null

来源:互联网 发布:python ui 编辑:程序博客网 时间:2024/06/05 04:05

– Start

package shangbo.spring.core.example26;public class Person {    private String name = "Shangbo";    private int age = 30;    //    // Setter    //    public void setName(String name) {        this.name = name;    }    public void setAge(int age) {        this.age = age;    }    public String toString() {        return "[name=" + name + ", age=" + age + "]";    }}
<?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">    <!--         注入 null     -->    <bean class="shangbo.spring.core.example26.Person">        <property name="name">            <null/>        </property>        <property name="age" value = "30"/>    </bean></beans>
package shangbo.spring.core.example26;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class App {    public static void main(String[] args) {        // 实例化 Spring IoC 容器        ApplicationContext context = new ClassPathXmlApplicationContext("example.xml", Person.class);        // 从容器中获得 Person 对象        Person p = context.getBean(Person.class);        // 使用对象        System.out.println(p);    }}

更多参见:Spring Framework 精萃
– 声 明:转载请注明出处
– Last Updated on 2017-06-17
– Written by ShangBo on 2017-05-22
– End

原创粉丝点击