bean命名空間和自動裝配

2022-09-10 09:00:14 字數 1198 閱讀 1983

1.命名空間

命名空間分為以下幾種。初始階段學習singleton(預設)和prototype

singleton(預設)和prototype的區別:

使用在定義bena的時候標籤裡面加上scope,在加上以上6種屬性即可:

student student = (student) context.getbean("student");

student student1 = (student) context.getbean("student");

singleton:通過這個bean建的物件,他們的hashcode是一樣的。每次從bean中get的時候都是同乙個物件。即student==student1

prototype:通過這個bean建的物件,他們的hashcode是不一樣一樣的。每次從bean中get的時候都是新new乙個物件。即student不等於student1

2.bean的自動裝配

bean有三種裝配方式

1.在xml中顯示配置-----前面一直在使用的

2.在j**a**中顯示配置

3.自動裝配bean【autowired】

配置環境:乙個人有兩個寵物   people類 dog類 cat類

byname自動裝配:會在容器的上下文去查詢和自己set方法後面值相同的beanid。即setdog(dog dog)  bean id=dog

bytypea自動裝配:會在容器的上下文去查詢和自己屬性相同的beanid。型別必須全域性唯一才能裝配。多個相同型別裝配失敗

Bean自動裝配

spring ioc容器可以自動裝配 autowire 來協作bean之間的相互聯絡。autowire一共有五種型別。由於autowire可以針對單個bean進行設定,因此可以讓有些bean使用autowire,有些bean不採用。autowire的方便之處在減少或者消除屬性或構造器引數的設定,這樣...

spring 自動裝配bean

在spring中,支援 5 自動裝配模式。public class customer public void setperson person person 要啟用 autowired,必須註冊 autowiredannotationbeanpostprocessor 可以使用在 set方法 構造方...

(Spring)自動裝配bean

自動裝配說明 spring的自動裝配需要從兩個角度來實現,或者說是兩個操作 元件掃瞄和自動裝配組合發揮巨大威力,使得顯示的配置降低到最少。public class cat public class dog public class people public void setcat cat cat ...