spring10: 引用类型的自动注入
生活随笔
收集整理的這篇文章主要介紹了
spring10: 引用类型的自动注入
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
package com.atChina.Test5;public class Student {private String name;private int age;private School school;public Student(){System.out.println("無參數構造方法...");}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.Test5;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;} }引用類型的自動注入--byName
<?xml version="1.0" encoding="UTF-8"?> <!-- 引用Spring的多個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.xsd "><!-- 引用類型的自動注入, 由框架給引用類型完成賦值,賦值的方式主要由byName,byType1. byName(按名稱注入): java類中引用類型的屬性名和spring容器(xml配置文件)中的<bean>的id名稱一樣,且數據類型是一樣的,這樣的bean對象能夠賦值給引用類型指定byName自動注入<bean id="xx" class="yy" autowire="byName"></bean>--><bean id="student" class="com.atChina.Test5.Student" autowire="byName"><property name="name" value="宋江"/><property name="age" value="20" /><!-- <property name="school" ref="xuexiao"/> --></bean><!-- id的值要與java類中的屬性名一致 --><bean id="school" class="com.atChina.Test5.School"><property name="name" value="同濟大學"/><property name="address" value="上海市" /></bean> </beans>?
?
?引用類型的自動注入--byType
package com.atChina.Test6;public class PrimarySchool extends School {} <?xml version="1.0" encoding="UTF-8"?> <!-- 引用Spring的多個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.xsd "><!-- 引用類型的自動注入, 由框架給引用類型完成賦值,賦值的方式主要由byName,byType1. byName(按名稱注入): java類中引用類型的屬性名和spring容器(xml配置文件)中的<bean>的id名稱一樣,且數據類型是一樣的,這樣的bean對象能夠賦值給引用類型指定byName自動注入<bean id="xx" class="yy" autowire="byName"></bean>2. byType(按類型注入):java類中引用類型的數據類型和spring容器(xml配置文件)中的<bean>的class屬性值是同源關系的,這樣的bean可以賦值給引用類型同源關系的:1. java類中引用類型的數據類型和<bean>的class是一樣的2. java類中引用類型的數據類型和<bean>的class是父類和子類的關系3 java類中引用類型的數據類型和<bean>的class是接口和實現類關系指定byType自動注入<bean id="xx" class="yy" autowire="byType"></bean>byType的自動注入注意的是,符合條件的對象只能有一個。--><bean id="student" class="com.atChina.Test6.Student" autowire="byType"><property name="name" value="吳用"/><property name="age" value="20" /><!-- <property name="school" ref="xuexiao"/> --></bean><!-- <bean id="xuexiao" class="com.atChina.Test6.School"><property name="name" value="同濟大學"/><property name="address" value="上海市" /></bean> --> <bean id="priSchool" class="com.atChina.Test6.PrimarySchool"><property name="name" value="實驗小學"/><property name="address" value="上海市" /></bean> </beans>?
總結
以上是生活随笔為你收集整理的spring10: 引用类型的自动注入的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: spring8: di依赖注入--构造注
- 下一篇: spring11:为应用指定多个spri