Spring框架——IOC

spring

作用:ioc容器,控制反轉(zhuǎn),將創(chuàng)建對象的權(quán)利交給容器去做
好處:不用new對象,降低了類與類之間的耦合度
功能:IOC+AOP+DATA+WEB

spring的原理

將bean的類名以及類與類的關(guān)系配置在xml文件中,通過反射的方式創(chuàng)建對象,并且組裝對象。

spring快速入門

1.導(dǎo)包
core、context、expression、bean

2.引入schema文檔(類似dtd文檔)來約束xml文檔

    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                        http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>

3.通過xml配置文件

     <bean class="com.hemi.bean.CandidateA" id="canA"/>
     <bean class="com.hemi.bean.CandidateB" id="canB"/>
      <bean class="com.hemi.bean.Personnal" id="personnal">
      <!-- 通過構(gòu)造函數(shù)將候選人注入到人事部中  name就是Personnal類中要傳進去的對象-->   
      <constructor-arg name="write" ref="canA"></constructor-arg>
      </bean>

4.創(chuàng)建測試類

public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
    Personnal personnal =(Personnal) context.getBean("personnal");
    personnal.interviw();
}

注入方式

-構(gòu)造函數(shù)方式注入:
-constructor-arg:構(gòu)造函數(shù)參數(shù)
-type:使用構(gòu)造函數(shù)參數(shù)類型
-name:使用構(gòu)造函數(shù)參數(shù)名
-index:使用位置 0代表構(gòu)造函數(shù)第一個位置,1代表第二個位置,以此類推

 <bean class="com.hemi.bean.Personnel" id="personnel">
        <constructor-arg index="0" ref="canB" />
        <constructor-arg index="1" ref="canA" />
    </bean>

-get set方法注入
-property代表屬性名稱
-value屬性值
-ref 對象的引用

   <bean class="com.hemi.bean.Personnel" id="personnel">
        <property name="name" value="lili"></property>
        <property name="programme" ref="canA"></property>
    </bean>

-p名稱空間
在文檔定義中添加xmlns:p="http://www.springframework.org/schema/p"

<bean class="com.hemi.bean.Personnel" id="personnel" p:name="lisi"></bean>

總結(jié)

spring ioc容器特點:
1.在啟動的時候會將所有的對象按順序創(chuàng)建完畢
2.按需注入
3.按需獲取

bean參數(shù)詳解

id:對象的名字
destory-method:ioc容器摧毀時創(chuàng)建
init-method:創(chuàng)建對象時執(zhí)行的方法
depends-on:創(chuàng)建對象之前應(yīng)該創(chuàng)建好的對象
lazy-init:延遲創(chuàng)建對象
scope:設(shè)置作用域:Singleton(單例)、prototype(多例)、request、session、global session factory-method:工廠方法 factory-bean:工廠對象
abstract:標(biāo)記為抽象類

注解創(chuàng)建對象

創(chuàng)建一個xml文件

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                        http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.springframework.org/schema/context 
                        http://www.springframework.org/schema/context/spring-context.xsd">
    <context:component-scan base-package="com.hemi.bean"></context:component-scan>
</beans>  

創(chuàng)建對象的注解
-@Component
-@Service
-@Reposotory
-Controller
用法:

//創(chuàng)建對象的時候可以使用參數(shù),設(shè)置對象的引用變量
//如果沒有寫,那么默認使用小駝峰命名
@Component("blackBox")
public class BlackBox{

}

note:四者用法一致,一般使用@Service

注解注入對象

注入對象的注解
-@Resource
-@Autowired

用法:

    //name:按照名稱來查找
    @Resource(name="blackBox")
    private IBox box;

    //type:按照類型來查找
    @Resource(type=A4Paper.class)
    private IPaper paper;

    //如果沒有寫,那么name就是參數(shù)的變量名 box,所以找不到,然后按照type來查找,IBox類型,所以可以找得到
    //如果沒有寫,而內(nèi)存中有多個相同類型的對象,那么就報錯
    @Resource
    private IBox box1;
    //@Autowired不能寫任何參數(shù)
    //按照類型來查找,如果內(nèi)存中有多個相同類型的對象,那么報錯
    //解決問題:使用@Qualifier來指定注入哪個名稱的對象
    @Autowired
    @Qualifier("blackBox")
    private IBox box;

note:用那個注解根據(jù)實際需求選擇

注入數(shù)組、集合

配置xml文件注入

    <bean class="com.hemi.bean.Classes" id="classes">
        <property name="name">
            <list>
                <value>lisi</value>
                <value>wangwu</value>
                <value>zhaoliu</value>
            </list>     
        </property>
        <property name="list">
            <list>
                <value>1</value>
                <value>2</value>
                <value>3</value>
            </list>
        </property>
        <property name="map">
            <map>
                <entry key="JDBCDriver" value="com.mysql.jdbc.dDriver"></entry>
                <entry key="username" value="root"></entry>
            </map>
        </property>
    </bean>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 文章作者:Tyan博客:noahsnail.com 3.4 Dependencies A typical ente...
    SnailTyan閱讀 4,492評論 2 7
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,272評論 6 342
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,554評論 19 139
  • 文章作者:Tyan博客:noahsnail.com 3.4 依賴 標(biāo)準(zhǔn)企業(yè)應(yīng)用不會由一個對象(或Spring用語中...
    SnailTyan閱讀 1,266評論 0 1
  • 「想必大家覺得早起應(yīng)該是一件很痛苦的事情吧」 你看,每天6點起來,簡直應(yīng)該是有強迫癥該干的事啊,讓你第一天6點起來...
    斜杠時光閱讀 1,075評論 0 1

友情鏈接更多精彩內(nèi)容