Spring自動裝配

2022-08-01 09:12:10 字數 1242 閱讀 5218

spring 的自動裝配 xml檔案配置的方式

<?xmlversion="1.0"encoding="utf-8"?>

xmlns=""

xmlns:xsi=""

xmlns:p=""

xsi:schemalocation=" /spring-beans.xsd"

default-autowire="byname">

1、通過autowrite="byname": student的teacher 屬性必須等於 teacher的id

1.id="teacher"class="cn.tri.vo.teacher"p:name="yefan"p:age="18">

2.id="stu"class="cn.tri.vo.student"p:id="6"p:name="liming"p:age="8"autowire="byname">

2、通過autowrite="bytype": bytype型別 會自動尋找乙個與teacher類的bean,但是型別相同的多個bean通過bytype注入會報錯

3、通過autowrite="constrcuctor": constrcutor根據構造方法尋找,其他bean的型別是否與該類的構造器引數方法一致

spring 的自動裝配 通過註解的方式:

@component("stu") 總註解

dao層註解:@respository

service層註解:@service

controller層註解:@controller

@component("teacherdaoimpl")    //id="teacherdaoimpl"的bean

public class teacherdaoimpl implements teacherdao

}等同於

Spring自動裝配

先在cn.csdn.hr.dao包中建立幾個檔案 public inte ce basedao public class basehibernatedaoimpl implements basedao public inte ce customerdao public class customerd...

Spring自動裝配

通常情況下,配置bean的時候,一些屬性依賴需要我們自己去裝配,但是如果這是乙個比較大型的工程,裡面定義了成百上千個bean,那全都靠我們手工去裝配,你一定打心底厭煩,幸運的是spring為我們提供了自動裝配的方式 只要遵循一定的規則,spring就會自動幫你裝配依賴,而不需要你手工去裝配 自動裝配...

Spring 自動裝配

bean的自動裝配 autowiring 四種型別 1.no 不做任何操作 2.byname 根據屬性名自動裝配。此選項將檢查容器並根據名字查詢與屬性完全一致的bean,並將其與屬性自動裝配 3.bytype 如果容器中存在乙個與指定屬性型別相同的bean,那麼將與該屬性自動裝配 如果存在多個該型別...