spring級聯(lián)屬性賦值的兩種方式解析
這篇文章主要介紹了spring級聯(lián)屬性賦值的兩種方式解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
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 + "]";
}
}
一、利用setter方法進行賦值
在bean中需要賦值的屬性必須要有setter方法,同時bean中必須還要有一個無參的構(gòu)造方法。如若不顯示聲明,則默認會有一個。
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>
關(guān)鍵就是標紅的兩個代碼:先進行關(guān)聯(lián),然后進行級聯(lián)賦值。
二、利用構(gòu)造方法進行級聯(lián)賦值
此時,要在Person中加一個有參構(gòu)造方法:
public Student(String name, int age, double score, Car car) {
super();
this.name = name;
this.age = age;
this.score = score;
this.car = car;
}
在Car中加一個無參構(gòu)造方法:
public Car() {
}
同時,對于這種方法,我們刪除掉Person中name、age、score屬性的getter和setter方法,保留car屬性的getter和setter方法,程序仍然是可行的。
在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>
總結(jié):
1.利用setter方法進行級聯(lián)屬性賦值需要:無參構(gòu)造方法、setter方法。
2.利用構(gòu)造器進行級聯(lián)屬性賦值需要:有參構(gòu)造方法。
3.為級聯(lián)屬性賦值,屬性先要初始化之后才可以為級聯(lián)屬性賦值,否則會有異常,即:
<property name="car" ref="car"></property> <constructor-arg ref="car"></constructor-arg>
而在struct2中則不用,它會自動進行初始化。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- springmvc級聯(lián)屬性處理無法轉(zhuǎn)換異常問題解決
- 詳解Hibernate cascade級聯(lián)屬性的CascadeType的用法
- spring源碼學(xué)習(xí)之bean的初始化以及循環(huán)引用
- mybatis中實現(xiàn)讓返回值與bean中字段相匹配
- SpringBoot普通類獲取spring容器中bean的操作
- 解決Spring Boot 多模塊注入訪問不到j(luò)ar包中的Bean問題
- 通過工廠模式返回Spring Bean方法解析
- Spring中BeanFactory和ApplicationContext的作用和區(qū)別(推薦)
- Spring內(nèi)部bean和級聯(lián)屬性用法詳解
相關(guān)文章
Java設(shè)計模式模板方法(Template)原理解析
這篇文章主要介紹了Java設(shè)計模式模板方法(Template)原理解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-11-11
關(guān)于Jackson的JSON工具類封裝 JsonUtils用法
這篇文章主要介紹了關(guān)于Jackson的JSON工具類封裝 JsonUtils用法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
MyEclipse+Tomcat+MAVEN+SVN項目完整環(huán)境搭建(圖文教程)
這篇文章主要介紹了MyEclipse+Tomcat+MAVEN+SVN項目完整環(huán)境搭建(圖文教程),非常具有實用價值,需要的朋友可以參考下2017-12-12
Redis結(jié)合AOP與自定義注解實現(xiàn)分布式緩存流程詳解
項目中如果查詢數(shù)據(jù)是直接到MySQL數(shù)據(jù)庫中查詢的話,會查磁盤走IO,效率會比較低,所以現(xiàn)在一般項目中都會使用緩存,目的就是提高查詢數(shù)據(jù)的速度,將數(shù)據(jù)存入緩存中,也就是內(nèi)存中,這樣查詢效率大大提高2022-11-11
項目打包成jar后包無法讀取src/main/resources下文件的解決
本文主要介紹了項目打包成jar后包無法讀取src/main/resources下文件的解決,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04

