在 《spring筆記1---基於spring框架的第乙個程式 》一文中,在spring-conf.xml中配置bean時,我們向這個bean傳入了一些引數,但是這些引數都是一些簡單資料型別,如int、string等,本文將說明如何將乙個bean作為乙個引數傳給另外乙個bena,即bean引用其他bean。
在《spring筆記1---基於spring框架的第乙個程式 》這篇文章基礎上,我們再進行如下修改:
1 新建乙個mobile類,這個類依賴user物件:
package com.gm.spring_test;
public class mobile
public void setuser(user user)
public string getmodel()
public void setmodel(string model)
public float getprice()
public void setprice(float price)
}
2 修改spring-conf.xml檔案:
<?xml version="1.0" encoding="utf-8"?>
3 使用bean:
mobile mobile = (mobile) context.getbean("mobile");
system.out.println(mobile.getuser());
Spring學習2 Bean的生命週期
1.使用beanfactory容器時 1 容器尋找bean的定義資訊並且將其實例化。2 使用依賴注入,spring按照bean定義資訊配置bean的所有屬性。3 如果bean實現了beannameaware介面,工廠呼叫bean的setbeanname 方法傳遞bean的id。4 如果bean實現了...
spring 引用其他Bean
spring ioc 容器中定義的bean 可以相互引用,ioc 充當媒介的作用。比如建立乙個乙個word 類,將car,people 等作為他的屬性。public class world public car getcar public void setcar car car public peo...
Spring 學習筆記 (三) Bean
配置 例項化 裝配 注入不同資料型別 基於annotation 定義 bean 標註在實現類上 bean 元件裝配 標註在屬性變數,屬性的 setter 方法,構造方法上 載入註解定義的 bean 區別見 difference between contextannotation config and...