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 閱讀(2109) 評論(0)  編輯  收藏


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


          網站導航:
           
          主站蜘蛛池模板: 馆陶县| 岚皋县| 正蓝旗| 海原县| 老河口市| 尖扎县| 康保县| 临漳县| 轮台县| 湟中县| 子洲县| 浦县| 东阳市| 青阳县| 锡林浩特市| 永登县| 西昌市| 桐城市| 武功县| 郧西县| 阳泉市| 南召县| 巍山| 松江区| 彭州市| 阜新| 化隆| 寻乌县| 会昌县| 凤凰县| 灵川县| 富顺县| 仁寿县| 边坝县| 贺州市| 祁阳县| 平顶山市| 枣庄市| 新建县| 淮安市| 财经|