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

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

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

    Java綠地(~ming~)

    Java 草地

    常用鏈接

    統計

    最新評論

    Spring的注入方式

    Spring

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


    二.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方法設置值

    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創建和分發:

    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":默認對象可以復用,false是不會加載的,因為創建多個實例就不需要了

    初始化的順序:構造函數(沒有就默認-àsetter-àinit-method



    3.
    內部類:<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會自動區分

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

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



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

     <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>

     

    三.構造函數的注入方式: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進行查找   //預先注入方式

    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>

    因為需要注入的屬性名和存在的要引用bean的名稱相同,所以可以用byName自動查找

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

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

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

     

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

    1.springDateAccessException:a.與特定持久化技術和實現無關

      b.應用與持久層框架解藕      c.RuntimeException,不需要強制捕獲  

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


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


    網站導航:
     
    主站蜘蛛池模板: 一边摸一边桶一边脱免费视频| 成年女人免费视频播放体验区| 亚洲国产成人久久综合区| 亚洲日本在线电影| 午夜成人免费视频| 精品剧情v国产在免费线观看 | 西西人体大胆免费视频| 亚洲成年人在线观看| 亚洲欧美自偷自拍另类视| 在线免费观看污网站| 亚洲国产aⅴ成人精品无吗| 亚洲av手机在线观看| 亚洲成a人片毛片在线| 国产人在线成免费视频| 亚洲日韩国产一区二区三区在线| 野花高清在线观看免费完整版中文 | 免费无码毛片一区二区APP| 亚洲妓女综合网99| 天天摸夜夜摸成人免费视频| 久久精品国产亚洲av瑜伽| 99在线热视频只有精品免费| 精品亚洲aⅴ在线观看| 两个人www免费高清视频| 国产免费av片在线无码免费看| 一级大黄美女免费播放| 久久亚洲国产精品五月天| 五月亭亭免费高清在线| 国产亚洲综合视频| 成人五级毛片免费播放| 疯狂做受xxxx高潮视频免费| 国产亚洲综合久久系列| 精精国产www视频在线观看免费| 亚洲AV日韩精品久久久久久 | 亚洲人6666成人观看| 8090在线观看免费观看| 久久精品国产亚洲av麻| 国产成人无码免费看片软件| 久久久久久亚洲Av无码精品专口| 女人被男人桶得好爽免费视频| jizz日本免费| 99999久久久久久亚洲|