Spring中在xml文件为对象赋值的几种方式

本文详细介绍了在Spring框架中通过property标签、constructor-arg标签以及p命名空间三种方式对Student类进行实例化的具体操作。展示了如何在XML配置文件中为Student类的不同属性赋值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值