Two ways of spring cascade attribute assignment
This article mainly introduces two methods of spring cascade attribute assignment analysis. The example code is introduced in great detail, which has a certain reference value for everyone's study or work. Friends in need can refer to it
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 + "]"; } }
1、 Assign value by setter method
The attribute to be assigned in the bean must have a setter method, and the bean must also have a parameterless constructor. If the declaration is not displayed, there will be one by 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 the two codes marked in red: association first, and then cascade assignment.
2、 Cascade assignment using construction method
At this time, add a parameter constructor in person:
public Student(String name,int age,double score,Car car) { super(); this.name = name; this.age = age; this.score = score; this.car = car; }
Add a parameterless construction method to car:
public Car() { }
At the same time, for this method, we delete the getter and setter methods of the name, age and score attributes in person and retain the getter and setter methods of the car attribute. The program is still feasible.
In ApplicationContext In 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>
Summary:
1. Cascade attribute assignment using setter method requires: parameterless construction method and setter method.
2. Using constructors to assign cascading attributes requires a parametric construction method.
3. Assign values to cascading attributes. The attributes must be initialized before assigning values to cascading attributes. Otherwise, there will be exceptions, that is:
<property name="car" ref="car"></property> <constructor-arg ref="car"></constructor-arg>
It is not used in struct2. It will be initialized automatically.
The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.