spring7: di依赖注入--设值注入
生活随笔
收集整理的這篇文章主要介紹了
spring7: di依赖注入--设值注入
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
?
di:就是依賴注入,給屬性賦值。
di注入的分類:
? ? ? ? 1.設(shè)值注入,調(diào)用java類中的set方法,給屬性賦值。
? ? ? ?2. 構(gòu)造注入,調(diào)用java類中的有參數(shù)構(gòu)造方法,創(chuàng)建對(duì)象的同時(shí),給屬性賦值。
?
di的語(yǔ)法:
? ? ? 1. 基于xml的配置文件,在xml中使用標(biāo)簽和屬性,完成屬性的賦值。
? ? ? 2.基于注解的方式,使用注解創(chuàng)建對(duì)象,給屬性賦值。
設(shè)值注入(set方法注入)
簡(jiǎn)單類型的設(shè)值注入
package com.atChina.Test;public class Student {private String name;private int age;public void setName(String name) {this.name = name;}public void setAge(int age) {this.age = age;}@Overridepublic String toString() {return "Student [name=" + name + ", age=" + age + "]";} } <?xml version="1.0" encoding="UTF-8"?> <!-- 引用Spring的多個(gè)Schema空間的格式定義文件 --> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd "><!-- 設(shè)值注入: 調(diào)用類中的set方法完成屬性賦值 簡(jiǎn)單類型: spring中把string和java基本數(shù)據(jù)類型,稱為簡(jiǎn)單類型簡(jiǎn)單類型的設(shè)值注入:<bean id="xx" class="yy"><property name="屬性名" value="簡(jiǎn)單類型的屬性值"/><property name="屬性名" value="簡(jiǎn)單類型的屬性值"/>...</bean>--><bean id="student" class="com.atChina.Test.Student"><property name="name" value="宋江"/><property name="age" value="20" /></bean> </beans>引用類型的設(shè)值注入:
<?xml version="1.0" encoding="UTF-8"?> <!-- 引用Spring的多個(gè)Schema空間的格式定義文件 --> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd "><!-- 設(shè)值注入: 調(diào)用類中的set方法完成屬性賦值 簡(jiǎn)單類型: spring中把string和java基本數(shù)據(jù)類型,稱為簡(jiǎn)單類型1)簡(jiǎn)單類型的設(shè)值注入:<bean id="xx" class="yy"><property name="屬性名" value="簡(jiǎn)單類型的屬性值"/><property name="屬性名" value="簡(jiǎn)單類型的屬性值"/>...</bean>2)引用類型的設(shè)值注入語(yǔ)法1: 使用ref作為屬性<bean id="xx" class="yy"><property name="屬性名" ref="bean的id"/></bean>語(yǔ)法2: 使用ref作為子標(biāo)簽<bean id="xx" class="yy"><property name="屬性名"><ref bean="bean的id"/><property/></bean>--><!-- 使用語(yǔ)法1,給引用類型賦值,ref作為屬性 --><bean id="student" class="com.atChina.Test2.Student"><property name="name" value="宋江"/><property name="age" value="20" /><property name="school" ref="xuexiao"/></bean><!-- 使用語(yǔ)法2,ref作為子標(biāo)簽性 --><bean id="student2" class="com.atChina.Test2.Student"><property name="name" value="吳用"/><property name="age" value="22" /><property name="school"><ref bean="xuexiao"/></property></bean><bean id="xuexiao" class="com.atChina.Test2.School"><property name="name" value="同濟(jì)大學(xué)"/><property name="address" value="上海市" /></bean> </beans> package com.atChina.Test2;public class Student {private String name;private int age;private School school;public Student(){System.out.println("無(wú)參數(shù)構(gòu)造方法...");}public void setSchool(School school) {this.school = school;}public void setName(String name) {this.name = name;}public void setAge(int age) {this.age = age;}@Overridepublic String toString() {return "Student [name=" + name + ", age=" + age + ", school=" + school+ "]";} } package com.atChina.Test2;public class School {private String address;private String name;public void setAddress(String address) {this.address = address;}@Overridepublic String toString() {return "School [address=" + address + ", name=" + name + "]";}public void setName(String name) {this.name = name;} }?
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的spring7: di依赖注入--设值注入的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: spring6:bean的生命始末方法
- 下一篇: spring8: di依赖注入--构造注