Spring注入方式坤溃、注入類型以及常用注解

注入方式

1.屬性setter注入(需要無參構(gòu)造方法):

對依賴對象寫入setter方法莱革,通過setter的對象注入容器中
        <!-- 屬性注入(setter注入)-->
        <!--將容器中的messageService卑惜,注入給當(dāng)前Controller的messageService屬性(通過該屬性的setter)-->
        <property name = "messageService" ref="messageServiceBean"/>
    public MessageController(){

    }    
    public void setMessageService(IMessageService messageService){
        this.messageService = messageService;
    }

2.構(gòu)造方法注入(有參構(gòu)造方法):

注入對象構(gòu)造方法中的參數(shù),使容器識別依賴對象
    <bean id="msgControllerBean" class="com.apescource.web.MessageController">
        <!-- 構(gòu)造注入(構(gòu)造方法注入)-->
        <constructor-arg name="messageService" ref="messageServiceBean"/>
        <constructor-arg name="defaultMsg" value="默認消息模板"/>
    </bean>
    public MessageController(String defaultMsg,IMessageService messageService){
        System.out.println(defaultMsg+":");
        this.messageService = messageService;
    }

3.接口注入

接口注入模式因為具備侵入性啥繁,它要求組件必須與特定的接口相關(guān)聯(lián)府瞄,因此并不被看好托猩,實際使用有限印蓖。

注入類型

1.注入字符串,單個數(shù)值類型

<bean id = "example" class = com.example>
    <property name = " " value = " "></property>
</bean>

2.注入bean京腥,引用類型

<bean id = "outerBean" class="com.outer">
    <property name = "target">
        <bean class = "com.Person">
            <property name = "name" value = "Amy">
            <property name = "age" value = "18">
        </bean>
    </property>
 </bean>

3.注入集合赦肃,List、set公浪、Map他宛、Properties等

<bean id = "listBean" class = "com.rep.example01">
    <property name = "list">
        <list>
            <value>一</value>
            <value>二</value>
            <value>三</value>
            <ref bean = "dataSource"/>
        </list>
    </property>
</bean>


<bean id = "setBean" class = "com.rep.example02">
    <property name = "set">
        <set>
            <value>張</value>
            <value>王</value>
            <value>李</value>
            <ref bean = "dataSource"/>
        </set>
    </property>
</bean>


<bean id = "mapBean" class = "com.rep.example03">
    <property name = "map">
        <map>
            <entry key = "an entry" value = "just some string"/>
            <entry key = "a ref" value = "dataSource"/>
        </map>
    </property>
</bean>


<bean id = "propsBean" class = "com.rep.example04">
    <property name = "props">
        <props>
            <prop key = "一">one</prop>
            <prop key = "二">two</prop>
            <prop key = "三">three</prop>
        <props>
    </property>
</bean>

Spring常用注解

1.@Component:類級別注解,標(biāo)注一個普通的spring Bean類欠气。表明該類會作為組件類厅各,并告知Spring要為這個類創(chuàng)建Bean。

@Componet
public class DataService{
    private IRepostitory repostitory;
    public boolean saveData(){...};
}

2.@ComponentScan:類級別注解预柒,使用在配置類队塘。啟用組件掃描,默認當(dāng)前配置類所在包為基礎(chǔ)包宜鸯。

//  基礎(chǔ)包
@ComponentScan(basePackages="com.codeup")
public class SystemSpringConfig{
    
}

//指定類所在包為基礎(chǔ)包
@ComponentScan(basePackageClasses = ICodeupMarker.class)
public class SystemSpringConfig{
    
}

3.@Autowired:方法級別注解憔古,自動注入一個符合類型要求的Bean。(參數(shù):required:是否為必須注入項)

@Autowired
public DataService(Irepostitory repostitory){
    this.repostitory = repostitory;
}

4.@Primary:類級別的注解淋袖,一般與@Component配合使用鸿市,在自動裝備時候,設(shè)置某個bean為首選。

@Component
@Primary
public class FileRepostitoryImpl implements Irepostitory{
    public boolean writeData(List<String> dataList){
    }
}

5.@Qualifier:方法級別注解(一般使用于:構(gòu)造器焰情、Setter方法陌凳、普通方法),指定所注入的bean的ID内舟。(參數(shù):value:所注入的bean的ID)

@Autowired
@Qualifier("fileReopstitoryImpl")
public void setRepostitory(IRepostitory repostitory){
    this.repostitory = repostitory;
}

6.@Scope:類級別聲明注解冯遂,定義Bean的作用域。(參數(shù):value:作用域參數(shù)值谒获、singleton、prototype等)

public class DataService{

}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末壁却,一起剝皮案震驚了整個濱河市批狱,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌展东,老刑警劉巖赔硫,帶你破解...
    沈念sama閱讀 221,695評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異盐肃,居然都是意外死亡爪膊,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,569評論 3 399
  • 文/潘曉璐 我一進店門砸王,熙熙樓的掌柜王于貴愁眉苦臉地迎上來推盛,“玉大人,你說我怎么就攤上這事谦铃≡懦桑” “怎么了?”我有些...
    開封第一講書人閱讀 168,130評論 0 360
  • 文/不壞的土叔 我叫張陵驹闰,是天一觀的道長瘪菌。 經(jīng)常有香客問我,道長嘹朗,這世上最難降的妖魔是什么师妙? 我笑而不...
    開封第一講書人閱讀 59,648評論 1 297
  • 正文 為了忘掉前任,我火速辦了婚禮屹培,結(jié)果婚禮上默穴,老公的妹妹穿的比我還像新娘。我一直安慰自己褪秀,他們只是感情好壁顶,可當(dāng)我...
    茶點故事閱讀 68,655評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著溜歪,像睡著了一般若专。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上蝴猪,一...
    開封第一講書人閱讀 52,268評論 1 309
  • 那天调衰,我揣著相機與錄音膊爪,去河邊找鬼。 笑死嚎莉,一個胖子當(dāng)著我的面吹牛米酬,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播趋箩,決...
    沈念sama閱讀 40,835評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼赃额,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了叫确?” 一聲冷哼從身側(cè)響起跳芳,我...
    開封第一講書人閱讀 39,740評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎竹勉,沒想到半個月后飞盆,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,286評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡次乓,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,375評論 3 340
  • 正文 我和宋清朗相戀三年吓歇,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片票腰。...
    茶點故事閱讀 40,505評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡城看,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出杏慰,到底是詐尸還是另有隱情析命,我是刑警寧澤,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布逃默,位于F島的核電站鹃愤,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏完域。R本人自食惡果不足惜软吐,卻給世界環(huán)境...
    茶點故事閱讀 41,873評論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望吟税。 院中可真熱鬧凹耙,春花似錦、人聲如沸肠仪。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,357評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽异旧。三九已至意述,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背荤崇。 一陣腳步聲響...
    開封第一講書人閱讀 33,466評論 1 272
  • 我被黑心中介騙來泰國打工拌屏, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人术荤。 一個月前我還...
    沈念sama閱讀 48,921評論 3 376
  • 正文 我出身青樓倚喂,卻偏偏與公主長得像,于是被迫代替她去往敵國和親瓣戚。 傳聞我的和親對象是個殘疾皇子端圈,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,515評論 2 359