久久久精品一区ed2k-女人被男人叉到高潮的视频-中文字幕乱码一区久久麻豆樱花-俄罗斯熟妇真实视频

spring?IOC-創(chuàng)新互聯(lián)

Spring概述

專注于為中小企業(yè)提供網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)大安市免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了超過千家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。

以下內(nèi)容僅講解spring IOC基本使用方法

spring IOC: 依賴注入

spring AOP:管理組件對象,維護(hù)對象關(guān)系。目的:降低組件耦合度

Spring web MVC:MVC設(shè)計(jì):架構(gòu)一個(gè)MVC結(jié)構(gòu)的WEB程序
Spring整合其他技術(shù):JDBC,Mybatis,Hibernate,Struts等。

spring?IOC

Spring IOC應(yīng)用:

以注入的方式應(yīng)用對象,實(shí)現(xiàn)組件解耦

a.管理對象:創(chuàng)建,初始化,釋放資源,銷毀

b.維護(hù)對象關(guān)系:采用注入方式建立對象關(guān)系 Dependency Injection(DI)依賴注入(依賴注入:set方法注入,構(gòu)造器注入)

c.搭建SpringIOC開發(fā)環(huán)境:引入相關(guān)jar包;在src中添加ApplicationContext.xml

一.Spring容器—>管理組件及對象關(guān)系

1.創(chuàng)建ApplicationContext對象

2.向applicationContext.xml配置

3.利用ApplicationContext對象getBean()

實(shí)例化ApplicationContext容器對象—> applicationContext.xml

spring?IOC

二.Spring創(chuàng)建Bean對象的控制

1.控制對象創(chuàng)建方式(使用范圍)

 在元素中使用scope屬性控制,scope可以支持singleton和prototype,默認(rèn)值是singleton。

 該組件在Spring容器中只有一個(gè)bean對象,ac.getBean(“id")無論調(diào)用getBean();多少次都返回同一個(gè)對象;

 scope為prototype則每次ac.getBean(“id”);返回一個(gè)新的對象。

2.指定對象初始化方法

 利用元素的init-method指定,當(dāng)創(chuàng)建bean對象后自動(dòng)執(zhí)行init-method方法。

<bean id="e2"
     class="SpringIOC.ExampleBean" scope="prototype" init-method="init">
</bean>

3.指定對象銷毀方法

 利用元素的destroy-method指定。

 滿足下面條件才有效:

 —組件對象為單例模式

 —調(diào)用AbstractApplicationContext容器對象的close()方法

4.控制單例對象創(chuàng)建時(shí)機(jī)

 在默認(rèn)情況下,單例對象是Spring容器創(chuàng)建時(shí)實(shí)例化;可以使用元素的lazy-init=true屬性將創(chuàng)建時(shí)機(jī)推遲到getBean()方法調(diào)用時(shí)。

public class ExampleBean {

    public ExampleBean()
    {
        System.out.println("--創(chuàng)建ExampleBean--");
    }

    public void init()
    {
        System.out.println("init..");
    }

    public void destroy()
    {
        System.out.println("destory object...");
    }

    public void executor(){
        System.out.println("調(diào)用executor方法");
    }
}
<bean id="e1"
     class="SpringIOC.ExampleBean" init-method="init" lazy-init="true" destroy-method="destroy">
</bean>
public class TestBean {

    public static void main(String[] args)
    {

        //創(chuàng)建Spring容器對象
        String conf = "applicationContext.xml";
        AbstractApplicationContext ac = new ClassPathXmlApplicationContext(conf);

        //從spring容器獲取e1,e2
        ExampleBean e1 = ac.getBean("e1", ExampleBean.class);
        e1.executor();
        ExampleBean e2 = ac.getBean("e1", ExampleBean.class);
        System.out.println(e1 == e2);//true

        ExampleBean e3 = ac.getBean("e2", ExampleBean.class);
        ExampleBean e4 = ac.getBean("e2", ExampleBean.class);
        System.out.println(e3 == e4);//false
        ac.close();//釋放Spring容器對象,自動(dòng)調(diào)用單例的destory-method
    }
}

IOC: Inversion of Control控制反轉(zhuǎn)

 改變了對象獲取方式,之前編碼方式采用new構(gòu)造方式獲取對象;IOC中采用了由容器創(chuàng)建對象之后注入進(jìn)來使用。只要修改配置就可以改變對象關(guān)系。

spring?IOC

三.自動(dòng)裝配(自動(dòng)注入)

1.自動(dòng)注入(autowire)

用于指定自動(dòng)注入規(guī)則??梢允褂胋yType,byName,constructor等。用于簡化注入配置。

使用byType類型匹配注入需要注意,有2個(gè)及其以上匹配會(huì)出現(xiàn)異常。

2.各種類型信息的注入配置格式

 a).注入字符串,數(shù)值單個(gè)數(shù)值

 *b).注入bean對象

<bean id="computer" class="SpringIOC.Computer">
   <!--信息set注入-->
   <property name="cpu" value="i 5"></property>
   <property name="hdd" value="索尼"></property>
   <property name="mainbord" value="華碩"></property>
</bean>

<bean id="phone" class="SpringIOC.Phone">
   <!--構(gòu)造器注入-->
   <constructor-arg index="0" value="高通">
   </constructor-arg>
   <constructor-arg index="1" value="2G">
   </constructor-arg>
</bean>

<bean id="student" class="SpringIOC.Student">
   <!--利用set注入computer-->
   <property name="c" ref="computer">
   </property>
   <!--利用set注入phone-->
   <property name="p" ref="phone">
   </property>
</bean>

<bean id="student1" class="SpringIOC.Student"
         autowire="byType">
</bean>

 c).注入集合list,set,map,properties

<!--int和list中不允許傳null值-->
<bean id="msg" class="SpringIOC.MessageBean">
    <property name="name">
        <null/>
    </property>
    <property name="age" value="18">
    </property>
    <property name="sex">
        <null/>
    </property>
    <property name="birth" value="2017-07-17">
    </property>
    <property name="friends">
        <list>
            <value>tom</value>
            <value>jack</value>
            <!--<value><null/></value>-->
        </list>
    </property>
    <property name="cities">
        <set>
            <value>hongkong</value>
            <value>shanghai</value>
        </set>
    </property>
    <property name="books">
        <map>
            <entry key="1001" value="Java基礎(chǔ)"></entry>
            <entry key="1002" value="JavaWeb開發(fā)"></entry>
        </map>
    </property>
    <property name="db">
        <props>
            <prop key="username">root</prop>
            <prop key="password">123456</prop>
            <prop key="driver">com.mysql.jdbc.Driver</prop>
        </props>
    </property>
</bean>

 d).spring表達(dá)式注入

   #{表達(dá)式}

   #{id名.屬性}或#{id名.key}

   如果時(shí)對象屬性,需要有g(shù)etXXX方法

   #{List對象id[0]}

<!--定義List對象-->
<util:list id="someList">
    <value>小樹</value>
    <value>靜汪</value>
</util:list>
<!--定義Set集合-->
<util:set id="someSet">
    <value>hongkong</value>
    <value>shanghai</value>
</util:set>
<util:map id="someMap">
    <entry key="10003" value="Think in Java"></entry>
    <entry key="10004" value="Spring"></entry>
</util:map>
<util:properties id="someProps">
    <prop key="username">#{dbParams.user}</prop>
    <prop key="password">#{dbParams.password}</prop>
    <prop key="databases">#{dbParams.databases}</prop>
</util:properties>

<!--讀取db.properties形成一個(gè)Properties對象-->
<util:properties id="dbParams"
    location="classpath:db.properties">
</util:properties>


<bean id="msg1" class="SpringIOC.MessageBean">
    <property name="name" value="#{dbParams['123']}">
    </property>
    <!--<property name="name" value="#{msg.name}">-->
    <!--</property>-->
    <property name="friends" ref="someList">
    </property>
    <property name="cities" ref="someSet">
    </property>
    <property name="books" ref="someMap">
    </property>
    <property name="db" ref="someProps">
    </property>
</bean>

db.properties:

=====

3.利用注解配置應(yīng)用IOC

 在JDK5.0時(shí)追加一些新特性

 注解:在類定義,方法定義,成員變量定義前面使用,格式為@注解標(biāo)記名。

   a).組件自動(dòng)掃描

        可以按指定的包路徑,將包下所有組件掃描,如果發(fā)現(xiàn)組件類定義前有以下標(biāo)記,會(huì)將組件掃面倒Soring容器。

<context:component-scan base-package="springIOC"/>

        @Component  //其他組件

        @Controller //控制層組件

        @Service  //業(yè)務(wù)層組件xxxService

        @Repository  //數(shù)據(jù)訪問層組件xxxDao

        @Named(需要引入第三方標(biāo)準(zhǔn)包)

        @Scope控制對象創(chuàng)建,默認(rèn)單例

        @PostConstruct指定init-method

        @PreDestroy指定destroy-method

   b).注入注解

        @Resource:可以在變量定義前或setXXX方法前應(yīng)用

        @AutoWired:可以在變量定義前或setXXX方法前應(yīng)用

        一般使用時(shí),功能等價(jià),都可以實(shí)現(xiàn)注入。

        如果不存在多個(gè)匹配類型,使用@Resurce或@Autowired都可以。

        如果存在多個(gè)匹配類型,建議按名稱注入

        @Resource(name=“指定名稱”)或

        @Autowired

        @Qualifier(“p”)

        使用建議:set注入建議用@Resource,構(gòu)造器建議用@Autowired

        自己編寫的組件建議使用注解配置;框架API只能用XML配置。

spring?IOC

spring配置:

<context:component-scan base-package="springIOC"/>
代碼:
@Component    //掃描ExampleBean組件,默認(rèn)id=exampleBean
//@Scope("prototype")//等價(jià)于<bean scope="">
@Scope("singleton")//等價(jià)于<bean scope="">
public class ExampleBean {

    @PostConstruct //等價(jià)于<bean init-method="">
    public void init(){
        System.out.println("初始化邏輯");
    }
    public void execute(){
        System.out.println("---執(zhí)行execute處理方法---");
    }

    @PreDestroy //scope為singleton并釋放容器資源時(shí),等價(jià)于<bean destroy-method="">
    public void destroy(){
        System.out.println("釋放資源");
    }
}
@Component("exam1")
public class ExampleBean1 {

    public void execute(){
        System.out.println("---執(zhí)行exampleBean1的execute---");
    }
}

test

public class ExampleBeanTest {


        public static void main(String[] args)
        {
            String conf = "applicationContext.xml";
            AbstractApplicationContext ac = new ClassPathXmlApplicationContext(conf);

            ExampleBean e1 = ac.getBean("exampleBean", ExampleBean.class);
            e1.execute();

            ExampleBean1 e2 = ac.getBean("exam1", ExampleBean1.class);
            e2.execute();


            ExampleBean ee = ac.getBean("exampleBean", ExampleBean.class);
            System.out.println(e1.toString());
            System.out.println(ee.toString());
            ac.close();



        }
}

運(yùn)行結(jié)果:

---執(zhí)行execute處理方法---

---執(zhí)行exampleBean1的execute---

springIOC.ExampleBean@548a102f

springIOC.ExampleBean@548a102f

釋放資源

分享名稱:spring?IOC-創(chuàng)新互聯(lián)
新聞來源:http://sd-ha.com/article38/shgpp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、面包屑導(dǎo)航網(wǎng)站建設(shè)、品牌網(wǎng)站制作、網(wǎng)頁設(shè)計(jì)公司、App設(shè)計(jì)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作