Two ways of spring cascade property assignment

Car.java

package com.gong.spring.beans;

public class Car {
    private String name;

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "Car [name=" + name + "]";
    }
    
}

Student.java

package com.gong.spring.beans;

public class Student {
    private String name;
    private int age;
    private double score;
    private Car car;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public double getScore() {
        return score;
    }
    public void setScore(double score) {
        this.score = score;
    }
    public Car getCar() {
        return car;
    }
    public void setCar(Car car) {
        this.car = car;
    }
    @Override
    public String toString() {
        return "Student [name=" + name + ", age=" + age + ", score=" + score + ", car=" + car + "]";
    }
    
    
}

First, using the setter method assignment

In the bean need to assign attributes must have a setter method, while the bean must also have a no-argument constructor. If it does not display a statement, there will be a default.

applicationContext.xml

<?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">
    
    <bean id="car" class="com.gong.spring.beans.Car"></bean>
    
    <bean id="student" class="com.gong.spring.beans.Student">
        <property name="name" value="tom"></property>
        <property name="age" value="12"></property>
        <property name="score" value="98.00"></property>            
        <property name="car" ref="car"></property>
        <property name="car.name" value="baoma"></property>
    </bean>
    

</beans>

The key is marked red two codes: first associate and then cascade assignment.

Second, the method using a cascade structure assignment

In this case, a parameter to the constructor Person added:

    public Student(String name, int age, double score, Car car) {
        super();
        this.name = name;
        this.age = age;
        this.score = score;
        this.car = car;
    }

Car added in a constructor with no arguments:

    public Car() {
    }

At the same time, with this method, we removed Person in the name, age, getter and setter methods score the property, retaining getter and setter methods car attributes, the program is still viable.

In the applicationContext.xml:

<?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">
    
    <bean id="car" class="com.gong.spring.beans.Car"></bean>
    
    <bean id="student" class="com.gong.spring.beans.Student">
        <constructor-arg value="tom"></constructor-arg>
        <constructor-arg value="12"></constructor-arg>
        <constructor-arg value="98.00"></constructor-arg>
        <constructor-arg ref="car"></constructor-arg>
        <property name="car.name" value="baoma"></property>
    </bean>
    

</beans>

to sum up:

1. The method of using setter cascade attribute assignment requires: constructor with no arguments, the setter method.

2. Use constructor cascade attribute assignment requires: YES constructor parameters.

3. Cascade property assignment, before that property must first cascade property assignment after initialization, otherwise there will be exceptions, namely:

<property name="car" ref="car"></property>
<constructor-arg ref="car"></constructor-arg>

In struct2 in then do not, it will be automatically initialized.

Guess you like

Origin www.cnblogs.com/xiximayou/p/12150173.html