这里先写一个学生类进行实验:

public class Student {
    private Integer id;
    private String name;
    private Integer age;
    private Long cellphone;

    public Student() {
    }

    public Student(Integer id, String name, Integer age, Long cellphone) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.cellphone = cellphone;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Long getCellphone() {
        return cellphone;
    }

    public void setCellphone(Long cellphone) {
        this.cellphone = cellphone;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", cellphone=" + cellphone +
                '}';
    }
}

1.使用property标签赋值

 <bean id="s1" class="com.atguigu.spring.test.Student">
        <property name="id" value="1001"></property>
        <property name="name" value="亚索"></property>
        <property name="age" value="35"></property>
        <property name="cellphone" value="17623232323"></property>
    </bean>

2.使用constructor-arg标签进行构造器赋值

<bean id="s2" class="com.atguigu.spring.test.Student">
        <constructor-arg value="1002"></constructor-arg>
        <constructor-arg value="瑞文"></constructor-arg>
        <constructor-arg value="28" index="2" type="java.lang.Integer"></constructor-arg>
        <constructor-arg value="17823237878"></constructor-arg>

    </bean>

3.使用p命名空间进行赋值操作
这里先加入p命名空间到xml文件:

 xmlns:p="http://www.springframework.org/schema/p"

然后就可以使用p命名空间进行赋值了

<bean id="s3" class="com.atguigu.spring.test.Student"
          p:id="1003"
          p:name="盖伦"
          p:age="25"
          p:cellphone="19823918932">

    </bean>

然后可以写个类测试一下:

public class TestBySpring {
    public static void main(String[] args) {
        //初始化容器
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        //通过getBean()获取对象
//        Person person = (Person)ac.getBean("person");
        //使用此方法获取对象时,要求spring所管理的此类型的对象只能有一个
//        Person person = ac.getBean(Person.class);
        Person person = (Person) ac.getBean("person");
        Object s1 = ac.getBean("s1");
//        Object s2= ac.getBean("s2");
        Student s2 = ac.getBean("s2", Student.class);
        Student s3 = ac.getBean("s3",Student.class);
        System.out.println(person);
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);

        ((ClassPathXmlApplicationContext) ac).close();
    }
}
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐