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

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

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();
    }
}
Spring框架是一个应用程序框架,主要目的是简化Java开发。Spring容器是Spring框架的核心,它负责管理应用程序的所有对象Spring容器在启动时会创建所有需要的对象,并将它们初始化和配置好,最后将它们放入容器,供应用程序使用。Spring容器的生命周期包括实例化、属性赋值、初始化、销毁等几个阶段。 1. 实例化 Spring容器在启动时会读取配置文件,找到需要创建的对象,然后使用Java反射机制创建这些对象Spring支持多种实例化方式,包括构造方法实例化、工厂方法实例化、静态工厂方法实例化等。 2. 属性赋值 Spring容器在创建对象后,会自动将对象的属性赋值。属性赋值有两种方式:使用setter方法和使用注解。使用setter方法时,Spring容器会调用对象的setter方法,将配置文件的属性值赋值对象的属性。使用注解时,需要在属性上添加相应的注解,Spring容器会自动将配置文件的属性值赋值给注解标注的属性。 3. 初始化 Spring容器在完成属性赋值后,会调用对象的初始化方法。初始化方法可以使用注解@PostConstruct或实现接口InitializingBean来定义。在初始化方法,可以进行一些额外的初始化工作。 4. 销毁 Spring容器在关闭时,会调用对象的销毁方法。销毁方法可以使用注解@PreDestroy或实现接口DisposableBean来定义。在销毁方法,可以进行一些额外的销毁工作。 下面是Spring容器实例化和属性赋值的源码示例: ```java public class Student { private String name; private int age; public Student() { System.out.println("Student被创建了"); } public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } public void init() { System.out.println("Student被初始化了"); } public void destroy() { System.out.println("Student被销毁了"); } } public class StudentService { private Student student; public StudentService() { System.out.println("StudentService被创建了"); } public void setStudent(Student student) { this.student = student; } public void init() { System.out.println("StudentService被初始化了"); } public void destroy() { System.out.println("StudentService被销毁了"); } } public class Main { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); StudentService studentService = (StudentService) context.getBean("studentService"); System.out.println("学生姓名:" + studentService.getStudent().getName()); System.out.println("学生年龄:" + studentService.getStudent().getAge()); } } ``` 在上面的代码,Student类表示学生对象,StudentService类表示学生服务,Main类是启动Spring容器的入口。在Spring配置文件,定义了一个id为"student"的学生对象和一个id为"studentService"的学生服务对象,学生服务对象依赖于学生对象。在初始化时,Spring容器会自动为学生对象和学生服务对象赋值,并调用它们的初始化方法。在销毁时,Spring容器会调用它们的销毁方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值