日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

IOC操作Bean管理XML方式(xml自动装配)

發布時間:2024/7/23 asp.net 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 IOC操作Bean管理XML方式(xml自动装配) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目錄

?

IOC操作Bean管理XML方式(xml自動裝配)

一.演示自動裝配過程

(1)根據 屬性名稱 裝配

步驟一:

步驟二:

步驟三:?

(2)根據 屬性類型 裝配

? ? ? ?(2.1)根據 類型 自動裝配產生的問題

解決方法:


?

IOC操作Bean管理XML方式(xml自動裝配)

手動裝配:

之前寫的代碼中可以往一個類中注入屬性,做法就是:在xml配置文件中,通過property標簽中的name屬性,包括value屬性值,向類中設置值,這種方式叫做 手動裝配

自動裝配:

直接根據裝配規則【屬性名稱或者屬性類型】,不需要明確指定是什么名稱或者什么類型,Spring自動將匹配的屬性值進行注入,這種方式叫做 自動裝配

?

一.演示自動裝配過程

(1)根據 屬性名稱 裝配

步驟一:

創建一個autowire包,寫入兩個類,員工類Employee類,部門類Department類

結構如下:

?

Employee類代碼如下:

package com.lbj.spring5.autowire;public class Employee {//寫入部門對象//效果 在Employee中注入Department對象private Department department;public void setDepartment(Department department) {this.department = department;}//加入toString方法方便輸出@Overridepublic String toString() {return "Employee{" +"department=" + department +'}';}//加入測試方法方便測試public void test(){System.out.println(department);}}

?

?Department類

package com.lbj.spring5.autowire;public class Department { }

?

?步驟二:

?新建一個bean5.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"xmlns:p="http://www.springframework.org/schema/p"xmlns:util="http://www.springframework.org/schema/util"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"><!--實現自動裝配bean 標簽屬性 autowire,配置自動裝配autowire屬性常用兩個值byName 根據屬性名稱注入,注入的id值和類屬性名稱一樣byType 根據屬性類型注入 --><bean id="employee" class="com.lbj.spring5.autowire.Employee" autowire="byName"><!--<property name="department" ref="department"></property>--></bean><bean id="department" class="com.lbj.spring5.autowire.Department"></bean></beans>

測試類:

package com.lbj.spring5.testdemo;import com.lbj.spring5.autowire.Employee; import com.lbj.spring5.bean.Orders; import com.lbj.spring5.collectiontype.Book; import com.lbj.spring5.collectiontype.Course; import com.lbj.spring5.collectiontype.Student; import com.lbj.spring5.factorybean.Mybean; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestSpring5Demo1 {@Testpublic void tsetBean2(){ApplicationContext context=new ClassPathXmlApplicationContext("bean5.xml");Employee employee=context.getBean("employee", Employee.class);System.out.println(employee);} }

?

步驟三:?

測試結果 :

?

(2)根據 屬性類型 裝配

bean5.xml去Employee類中找到Department類的類型去裝配

?

bean5.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"xmlns:p="http://www.springframework.org/schema/p"xmlns:util="http://www.springframework.org/schema/util"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"><!--實現自動裝配bean 標簽屬性 autowire,配置自動裝配autowire屬性常用兩個值byName 根據屬性名稱注入,注入的id值和類屬性名稱一樣byType 根據屬性類型注入--><bean id="employee" class="com.lbj.spring5.autowire.Employee" autowire="byType"><!--<property name="department" ref="department"></property>--></bean><bean id="department" class="com.lbj.spring5.autowire.Department"></bean></beans>

?

測試結果:?

?

(2.1)根據 類型 自動裝配產生的問題

根據 類型 自動裝配時:相同類型的bean對象,不能定義多個,否則類型識別不出來是哪個bean注入

如下所示:?

?

解決方法:

1.使用?注解

2.使用?屬性名稱 裝配

?

?

?

?

?

?

總結

以上是生活随笔為你收集整理的IOC操作Bean管理XML方式(xml自动装配)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。