海鷗航際

          JAVA站
          posts - 11, comments - 53, trackbacks - 1, articles - 102

          Spring中IOC的實現

          Posted on 2005-01-14 20:30 海天一鷗 閱讀(615) 評論(0)  編輯  收藏 所屬分類: J2EE

           

          public class RefBean {
                 
          public String getAddress() {
                        
          return address;
                 }

                 
          public void setAddress(String address) {
                        
          this.address = address;
                 }

                 
          public String getZipcode() {
                        
          return zipcode;
                 }

                 
          public void setZipcode(String zipcode) {
                        
          this.zipcode = zipcode;
                 }

                 
          private String zipcode=null;
                 
          private String address=null;
          }

          Spring中IOC貫穿了其整個框架,但正如martinflower所說:“saying that these lightweight containers are special because they use inversion of control is like saying my car is special because it has wheels”,IOC已經稱為框架設計中必不可少的部分。就實現上來講Spring采取了配置文件的形式來實現依賴的注射,并且支持Type2 IOC(Setter Injection)以及Type3 IOC(Constructor Injection)。

              Spring中IOC的實現的核心是其Core Bean Factory,它將框架內部的組件以一定的耦合度組裝起來,并對使用它的應用提供一種面向服務的編程模式(SOP:Service-Orient Programming),比如Spring中的AOP、以及持久化(Hibernate、ibatics)的實現。

              首先從最底層最基礎的factory Bean開始,先來看org.springframework.beans.factory.Bean

              Factory接口,它是一個非常簡單的接口,getBean方法是其中最重要的方法,Spring通常是使用xml來populate Bean,所以比較常用的是XMLFactoryBean。

              用一個簡單的示例看一下其用法。首先寫下兩個Bean類:

              ExampleBean 類:

          public class ExampleBean {
                 
          private String psnName=null;
                 
          private RefBean refbean=null;
                 
          private String addinfo=null;
                 
          public String getAddinfo() {
                        
          return getRefbean().getAddress()+getRefbean().getZipcode();
                 }

                 
          public String getPsnName() {
                        
          return psnName;
                 }

                 
          public void setPsnName(String psnName) {
                        
          this.psnName = psnName;
                 }

                 
          public void setRefbean(RefBean refbean) {
                        
          this.refbean = refbean;
                 }

                
          public RefBean getRefbean() {
                        
          return refbean;
                 }

                 
          public void setAddinfo(String addinfo) {
                        
          this.addinfo = addinfo;
                 }

          }

           

           RefBean類:

           

          public class RefBean {
                 
          public String getAddress() {
                        
          return address;
                 }

                 
          public void setAddress(String address) {
                        
          this.address = address;
                 }

                 
          public String getZipcode() {
                        
          return zipcode;
                 }

                 
          public void setZipcode(String zipcode) {
                        
          this.zipcode = zipcode;
                 }

                 
          private String zipcode=null;
                 
          private String address=null;
          }
          其xml配置文件 Bean.xml

           

           

          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
          "http://www.springframework.org/dtd/spring-beans.dtd"
          >
          <beans>
            
          <bean id="exampleBean" class="test.ExampleBean">
              
          <property name="psnName"><value>xkf</value></property>
              
          <property name="refbean">
                 
          <ref bean="refBean"/>
              
          </property>
            
          </bean>
            
          <bean id="refBean" class="test.RefBean">
            
          <property name="address"><value>BeiJing</value></property>
            
          <property name="zipcode"><value>100085</value></property>
            
          </bean>
          </beans>

          然后可以寫個測試類來測試,當然,需要Spring中的Spring-core.jar以及commons-logging.jar,當然在elipse中可以通過安裝spring-ide插件來輕松實現。

          public class Test {
                 
          public static void main(String[] args){
                        
          try{
                        Resource input 
          = new ClassPathResource("test/Bean.xml");
                        System.
          out.println("resource is:"+input);
                        BeanFactory factory 
          = new XmlBeanFactory(input);
                        ExampleBean eb 
          =
                        (ExampleBean)factory.getBean(
          "exampleBean");
                        System.
          out.println(eb.getPsnName());
                        System.
          out.println(eb.getAddinfo());
                 }

                 
          catch(Exception e){
                        e.printStackTrace();
                 }

          }
          這樣,通過BeanFactory的getBean方法,以及xml配置文件,避免了在test類中直接實例化ExampleBean,消除了應用程序(Test)與服務(ExampleBean)之間的耦合,實現了IOC(控制反轉)或者說實現了依賴的注射(Dependency Injection)。

           

           

          主站蜘蛛池模板: 安龙县| 曲周县| 新巴尔虎右旗| 扶余县| 白水县| 江源县| 宝丰县| 格尔木市| 鹿泉市| 吴忠市| 金阳县| 新田县| 广水市| 耒阳市| 武宁县| 玛曲县| 柳州市| 牡丹江市| 河西区| 安福县| 霍城县| 梁山县| 涪陵区| 洛浦县| 黄大仙区| 盐亭县| 鄯善县| 盖州市| 昌邑市| 汉川市| 元谋县| 哈巴河县| 曲麻莱县| 台南县| 白水县| 唐河县| 连云港市| 滁州市| 刚察县| 合阳县| 温州市|