<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Java綠地(~ming~)

    Java 草地

    常用鏈接

    統(tǒng)計(jì)

    最新評論

    Spring的注入方式

    Spring

    一.Spring是一個輕量級的Ioc( DI )AOP的簡化企業(yè)開發(fā)的容器框架


    二.set注入方式



    1.
    屬性注入:a. public class Message {private String title;private String content;  getter/setter…}

    b.public interface Printer { public void startup(); public void print();}

      c. public class PrinterImpl implements Printer {  private Message message;

    public void setMessage(Message message){ …}   //通過ser方法設(shè)置值

    public void print() {System.out.println(message.getTitle());  }…….//需要值的存在

      d.屬性注入:<bean id="message" class="assemble.Message">  //將控制交給容器注入

                 <property name="title" value="china title"></property>

                 <property name="content" value="control content"></property>  </bean>


    e.
    通過底層的BeanFactory創(chuàng)建和分發(fā):

    XmlBeanFactory factory =new XmlBeanFactory(new ClassPathResource(“mypck/xxx.xml”));

    ClassName name=( ClassName) factory.getBean("className "); //get此時延時加載,功能有限



    2
    .引用其他類:<bean id="printer" class="assemble.PrinterImpl"

                    init-method="startup" singleton="true" >   //容器參與了生命周期管理

                   <property name="message" ref="message"></property>  </bean>

      singleton="true":默認(rèn)對象可以復(fù)用,false是不會加載的,因?yàn)閯?chuàng)建多個實(shí)例就不需要了

    初始化的順序:構(gòu)造函數(shù)(沒有就默認(rèn)-àsetter-àinit-method



    3.
    內(nèi)部類:<bean id="printer2" class="assemble.PrinterImpl"   init-method="startup">

         <property name="message">       <bean class="assemble.Message">

             <property name="title" value="china title2"></property>   </bean>

         </property>   </bean>



    4
    .裝配集合:a.public class Foo {    private List list;    private String[ ] array;

         private Set set;   private Map map;  private Properties props; setter/getter }

      b. <property name="list">//value值的類型spring會自動區(qū)分

           <list>    <value>spring</value>  <ref bean="message"/> </list>   </property>

         <property name="array">  <list>      <value>ejb</value>   </list>  </property>



    5.
    裝配set: <property name="set">       //set集合值是沒有重復(fù)的

     <set>   <value>jave ee</value>    <value>.net</value>    </set>  </property>



    6.
    裝配map: <property name="map">

          <map> <entry key="orm"><value>hibernate</value> </entry>  </map>  </property>



    7.
    裝配properties: <property name="props">  //propskeyvalue都是string所以可以簡寫

           <props>  <prop key="mvc">struts</prop>    </props>    </property>



    8.
    裝配null:<property name=”onename”> <null/> </property>

     

    三.構(gòu)造函數(shù)的注入方式:a. public class Address {   private String street; }

    b.public class Person {   private String name; 

       private Address address;   private Integer age; public Person() {  }

    public Person(String name, Address address) {

    this.name = name;    this.address = address;  }

     c. <bean id="person" class="assemble.Person">

         <constructor-arg index="0" value="liming"></constructor-arg>

         <constructor-arg index="1">

           <bean class="assemble.Address">

             <property name="street" value="hefei"></property>    </bean>

         </constructor-arg>

         <property name="age" value="22"></property>   </bean> // 可混合使用

    d:通過ApplicationContext進(jìn)行查找   //預(yù)先注入方式

    ApplicationContext ctx=new ClassPathXmlApplicationContext(“mypack/ClassName”);

    ClassName name=( ClassName) ctx.getBean("className "); 

     


    .自動裝配方式:主要是byNamebyType, 其他如constructor較少

      1.a.如果存在: <bean id="message" class="assemble.Message">

         <property name="content" value="control content"></property>  </bean>

    b.byName: <bean id="printer" class="assemble.PrinterImpl" >

                    <property name="message" ref="message"></property>  </bean>

    因?yàn)樾枰⑷氲膶傩悦痛嬖诘囊?/span>bean的名稱相同,所以可以用byName自動查找

    <bean id="printer" class="assemble.PrinterImpl" autowire="byName"/>

       2.byType:在容器中尋找一個與需要自動裝配的屬性類型相同的bean,如果找到超過一個相符的bean就會拋出異常.

    :如果配置文件中所有的bean的自動裝配方式相同,可以在<beans>根節(jié)點(diǎn)聲明以減少代碼,如果在自動裝配方式中再通過set方式注入,set方式會覆蓋autowire.

     

    :spring持久化支持:為整合如jdbc會拋出SQLException,Hibernate拋出HibernateException

    1.springDateAccessException:a.與特定持久化技術(shù)和實(shí)現(xiàn)無關(guān)

      b.應(yīng)用與持久層框架解藕      c.RuntimeException,不需要強(qiáng)制捕獲  

    posted on 2007-08-16 21:43 mrklmxy 閱讀(2112) 評論(0)  編輯  收藏


    只有注冊用戶登錄后才能發(fā)表評論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 亚洲国产精品va在线播放| 亚洲国产aⅴ成人精品无吗| 亚洲香蕉免费有线视频| 久久久国产亚洲精品| 一区国严二区亚洲三区| 久久精品电影免费动漫| 国产亚洲精品VA片在线播放| 亚洲国产成人精品91久久久| 国产精品免费高清在线观看| 香蕉大伊亚洲人在线观看| 亚洲色成人中文字幕网站| 久久午夜免费视频| 亚欧国产一级在线免费| 亚洲国产成人91精品| 亚洲伊人久久综合影院| 永久黄色免费网站| 无码 免费 国产在线观看91| 日木av无码专区亚洲av毛片| 免费国产综合视频在线看| 99久久久国产精品免费牛牛| 阿v视频免费在线观看| 亚洲国产美女视频| 亚洲熟妇av一区二区三区漫画| 在线观看特色大片免费视频| 两性色午夜视频免费播放| 亚洲日韩乱码中文字幕| 亚洲精品高清久久| 国产精品亚洲高清一区二区 | 久久水蜜桃亚洲AV无码精品| 亚洲av永久无码精品漫画 | 久久精品国产亚洲一区二区三区| 69xx免费观看视频| 免费国产草莓视频在线观看黄| 亚洲欧洲日本国产| 亚洲国产AV无码专区亚洲AV| 免费又黄又爽又猛的毛片| 99久久国产热无码精品免费 | 久久午夜夜伦鲁鲁片免费无码影视| 亚欧国产一级在线免费| 亚洲国产成人AV网站| 亚洲一区二区三区免费视频|