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

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

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

    隨筆-124  評論-49  文章-56  trackbacks-0
    IOC控制反轉:應用本身不負責依賴對象的創建及維護,依賴對象的創建及維護是由外部容器負責的。
    依賴注入:在運行期,由外部容器動態地將依賴對象注入到組件中。
    -----------------------------------------------------------------------------
    第一個項目
    1    spring的依賴庫
         * SPRING_HOME/dist/spring.jar
         * SPRING_HOME/lib/jakarta-commons/commons-logging.jar
         * SPRING_HOME/lib/log4j/log4j-1.2.14.jar
         如果使用了切面編程(AOP),還需要下列jar文件
         * SPRING_HOME/lib/aspectj/aspectjweaver.jar和aspectjrt.jar
         * SPRING_HOME/lib/cglib/cglib-nodep-2.1_3.jar
         如果使用了JSR-250的注解,如@Resource/@postConstruct/@preDestroy,還需要下列jar文件
         * SPRING_HOME/lib/j2ee/common-annotations.jar
    2    拷貝spring配置文件到src下
    3    拷貝log4j配置文件到src下
    4(1) 在UserManager中提供構造函數,spring將實例化好的UserDao實現注入給我們
    public class UserManagerImpl implements UserManager {
                    
    private UserDao userDao;
                    
    public UserManagerImpl(UserDao userDao){
                        
    this.userDao=userDao;
                    }

                    
    public void save(String username, String password) {
                        
    this.userDao.save(username, password);
                    }

              }
    4(2) 在UserManager中提供set方法,spring將實例化好的UserDao實現注入給我們
    public class UserManagerImpl implements UserManager {
                    
    private UserDao userDao;
                    
    public UserDao getUserDao() {
                        
    return userDao;
                    }

                    
    public void setUserDao(UserDao userDao) {
                        
    this.userDao = userDao;
                    }

                    
    public void save(String username, String password) {
                        
    this.userDao.save(username, password);
                    }

             }
    5    讓spring管理我們的對象創建和依賴,必須在spring配置文件中(applicationContext.xml)進行定義
         * 構造函數
    <bean id="userDao4MySqlImpl" class="com.my.spring.dao.UserDao4MySqlImpl"/>
           
    <bean id="userDao4OracleImpl" class="com.my.spring.dao.UserDao4OracleImpl"/>
           
    <bean id="userManagerImpl" class="com.my.spring.manager.UserManagerImpl">
                
    <constructor-arg ref="userDao4MySqlImpl"/>
                //
    <constructor-arg index="0" type="com.my.spring.dao.UserDao4OracleImpl" ref="userDao4MySqlImpl"/>
           
    </bean>
           * set方法
           方法一:
           
    <bean id="userDao4MySqlImpl" class="com.my.spring.dao.UserDao4MySqlImpl"/>
           
    <bean id="userDao4OracleImpl" class="com.my.spring.dao.UserDao4OracleImpl"/>
           
    <bean id="userManagerImpl" class="com.my.spring.manager.UserManagerImpl">
                
    <property name="userDao" ref="userDao4OracleImpl"/>
           
    </bean>
           方法二(使用內部bean)
           
    <bean id="userManagerImpl" class="com.my.spring.manager.UserManagerImpl">
                
    <property name="userDao">
                     
    <bean class="com.my.spring.manager.UserManagerImpl">
                
    </property>
           
    </bean>
    * 注解方式
          1 頭文件中必須加入
    xmlns:context="http://www.springframework.org/schema/context"
              http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
              
    <context:annotation-config/>//開啟注解
    2 在java代碼中使用@Autowired或@Resource注解方式進行裝配,它們區別是:
             * @Autowired默認按類型裝配,默認情況下要求依賴對象必須存在,如果允許null值,可以設置它required為false,
             如果想使用按名稱裝配,可以結合@Qualifier注解一起使用,如下:
                @Autowired @Qualifier("userBean")
                private User user;
             * @Resource默認按名稱裝配,找不到匹配的bean才會按類型裝配。它和@Autowired一樣可以標注在字段或屬性的setter方法上。
             可以通過name屬性指定名稱,如果沒有指定name屬性,當注解標注在字段上,即默認取字段的名稱為name屬性名稱,當注注解
             標注在屬性的setter方法上,即默認取屬性名作為bean名稱尋找依賴對象。
             注意:一旦指定了name屬性,就只能按名稱裝配了。
                @Resource(name="userBean")
                private User user;
    6    實例化spring容器并調用相關方法
         //ApplicationContext ctx=new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
         BeanFactory factory =new ClassPathXmlApplicationContext("applicationContext.xml");
    7    獲取bean
    UserManager userManager=(UserManager)ctx.getBean("userManagerImpl");
           UserManager userManager
    =(UserManager)factory.getBean("userManagerImpl");

    8    調用業務碼
        userManager.save("張三", "123");
    spring IoC容器的關鍵點:
         * 必須將被管理的對象定義到spring配置文件中
         * 必須定義構造函數或setter方法,讓spring將對象注入過來
    手動添加schema文件,方法如下:
    windows->preferences->myeclipse->files and editors->xml->xmlcatalog

    -------------------------------------------------------------------------------------------------------------------
    普通屬性的注入
      *多個配置文件可以放入String[]數組中,調用factory=new ClassPathXmlApplicationContext(String[] array);
      *多個配置文件可以使用*通配符,(測試未成功)factory=new ClassPathXmlApplicationContext("applicationContext-*.xml");
      *可以將一個類分成多個配置文件進行配置,但id不能相同
    示例:

    Beans:
    private String strValue;
        
    private int intValue;
        
    private List listValue;
        
    private Set setValue;
        
    private String[] arrayValue;
        
    private Map mapValue;
        
    private Properties properties;
    applicationContext-beans.xml
    <bean id="beans" class="com.my.spring.Beans">
            
    <property name="strValue" value="Hello"/>
            
    <property name="intValue">
                
    <value>123</value>
            
    </property>
            
    <property name="listValue">
                
    <list>
                    
    <value>list1</value>
                    
    <value>list2</value>
                
    </list>
            
    </property>
            
    <property name="setValue">
                
    <set>
                    
    <value>set1</value>
                    
    <value>set2</value>
                
    </set>
            
    </property>
            
    <property name="arrayValue">
                
    <list>
                    
    <value>array1</value>
                    
    <value>array2</value>
                
    </list>
            
    </property>
            
    <property name="mapValue">
                
    <map>
                    
    <entry key="k1" value="v1"/>
                    
    <entry key="k2" value="v2"/>
                
    </map>
            
    </property>
            
    <property name="date">
                
    <value>2008-08-15</value>
            
    </property>
            
    <property name="properties">
                
    <props>
                    
    <prop key="key1">value1</prop>
                    
    <prop key="key2">value2</prop>
                    
    <prop key="key3">value3</prop>
                
    </props>
            
    </property>
        
    </bean>
    調用:
    private BeanFactory factory=new ClassPathXmlApplicationContext("applicationContext-beans.xml");
            Beans b
    =(Beans)factory.getBean("beans");
            System.out.println(b.getIntValue());
            System.out.println(b.getStrValue());
            System.out.println(b.getArrayValue());
            System.out.println(b.getListValue());
            System.out.println(b.getMapValue());
            System.out.println(b.getDate());

    ---------------------------------------------------------------------------------------------
    自定義屬性編輯器
    *作用:將spring配置文件中的字符串轉換成相應的對象進行注入
           spring已經有內置的屬性編輯器,我們可以根據需求自己定義屬性編輯器
    *定義方法:
           1 繼承PropertyEditorSupport類,復寫setAsText()方法
           2 將屬性編輯注冊到spring中
    1 編寫自定義處理類
    public class UtilDatePropertyEditor extends PropertyEditorSupport {
        
    private String format="yyyy-MM-dd";
        @Override
        
    public void setAsText(String text) throws IllegalArgumentException {
            SimpleDateFormat sdf
    =new SimpleDateFormat(format);
            
    try {
                Date d
    =sdf.parse(text);
                
    this.setValue(d);
            }
     catch (ParseException e) {
                
    // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

        
    public String getFormat() {
            
    return format;
        }

        
    public void setFormat(String format) {
            
    this.format = format;
        }

    }
    2 配置apllicationContext-edition.xml
    <bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
            
    <property name="customEditors">
                
    <map>
                    
    <entry key="java.util.Date">
                        
    <bean class="com.my.spring.UtilDatePropertyEditor"/>
                    
    </entry>
                
    </map>
            
    </property>
    </bean>
    3 調用
    String[] contexts=new String[]{"applicationContext-beans.xml","applicationContext-edition.xml"};
            factory
    =new ClassPathXmlApplicationContext(contexts);

    -----------------------------------------------------------------------------------------------------
    公共屬性的注入
    依賴對象的注入方法,可以采用:
     * ref屬性
     * <ref>標簽
     * 內部<bean>來定義
    <bean id="beans2" class="com.my.spring.Beans2">
            
    <property name="beans3">
                
    <ref bean="beans3"/>
            
    </property>
            
    <property name="beans4" ref="beans4"/>
            
    <property name="beans5" ref="beans5"/>
    </bean>
    如何將公共的注入定義描述出來?
     * 通過<bean>標簽定義的屬性,指定abstract="true"
     * 具有相同屬性的類在<bean>標簽中指定其parent屬性
    <bean id="beanAbstract" abstract="true">
            
    <property name="id" value="1000"/>
            
    <property name="name" value="jack"/>
    </bean>
        
    <bean id="beans3" class="com.my.spring.Beans3" parent="beanAbstract">
            
    <property name="password" value="8888"/>
    </bean>

    <bean id="beans4" class="com.my.spring.Beans4" parent="beanAbstract"/>

    <bean id="beans2" class="com.my.spring.Beans2">
            
    <property name="beans3">
                
    <ref bean="beans3"/>
            
    </property>
            
    <property name="beans4" ref="beans4"/>
            
    <property name="beans5" ref="beans5"/>
    </bean>

    <bean id="beans5" class="com.my.spring.Beans5">
            
    <property name="age" value="123"/>
    </bean>

    --------------------------------------------------------------------------------------------------

    Bean的作用域

    scope可以取值:
      * singleton:每次調用getBean時返回相同的實例
      * prototype:每次調用getBean時返回不同的實例
     
    <bean id="beans" class="com.my.spring.Beans" scope="singletion">
    <bean id="beans" class="com.my.spring.Beans" scope="prototype">
    --------------------------------------------------------------------------------------------------
    自動裝配
    1 根據名稱
      設置<beans>標簽中default-autowire="byName"

    <bean id="beanAbstract" abstract="true">
                    
    <property name="id" value="1000"/>
                    
    <property name="name" value="jack"/>
            
    </bean>
            
    <bean id="beans3" class="com.my.spring.Beans3" parent="beanAbstract">
                    
    <property name="password" value="8888"/>
            
    </bean>
            
    <bean id="beans4" class="com.my.spring.Beans4" parent="beanAbstract"/>
            
            
    <bean id="beans2" class="com.my.spring.Beans2">
        
    <!-- 可以省略,會按名稱自動裝配
                    <property name="beans3">
                        <ref bean="beans3"/>
                    </property>
                    <property name="beans4" ref="beans4"/>
                    <property name="beans5" ref="beans5"/>
        
    -->
            
    </bean>
            
            
    <bean id="beans5" class="com.my.spring.Beans5">
                    
    <property name="age" value="123"/>
            
    </bean>
    2 根據類型
     設置<beans>標簽中default-autowire="byType"
    <bean id="beanAbstract" abstract="true">
                    
    <property name="id" value="1000"/>
                    
    <property name="name" value="jack"/>
            
    </bean>
            
    <bean id="beans33" class="com.my.spring.Beans3" parent="beanAbstract">
                    
    <property name="password" value="8888"/>
            
    </bean>
            
    <bean id="beans433" class="com.my.spring.Beans4" parent="beanAbstract"/>
            
            
    <bean id="beans2" class="com.my.spring.Beans2">
        
    <!-- 可以省略,會按類型自動裝配
                    <property name="beans3">
                        <ref bean="beans3"/>
                    </property>
                    <property name="beans4" ref="beans4"/>
                    <property name="beans5" ref="beans5"/>
        
    -->
            
    </bean>
            
            
    <bean id="beans523" class="com.my.spring.Beans5">
                    
    <property name="age" value="123"/>
            
    </bean>

    ------------------------------------------------------------------------------------------

    <Bean>屬性
    1 id為bean的名稱,是唯一的
    2 name也是為bean取名的,作用相同,區別是id中不能有"/"符號,name中可以有,如:
        * name="/login"
    3 class是bean類的全名
    4 init-method:bean的初始化方法
    5 destroy-method:bean消滅時調用的方法

    自動裝配依賴對象,不建意使用[byType/byName]
    <bean id="..." class="..." autowire="byType"/>

    ------------------------------------------------------------------------------------------------
    注解方式

    1 spring的依賴庫
         * SPRING_HOME/dist/spring.jar
         * SPRING_HOME/lib/jakarta-commons/commons-logging.jar
         * SPRING_HOME/lib/log4j/log4j-1.2.14.jar
         如果使用了切面編程(AOP),還需要下列jar文件
         * SPRING_HOME/lib/aspectj/aspectjweaver.jar和aspectjrt.jar
         * SPRING_HOME/lib/cglib/cglib-nodep-2.1_3.jar
         如果使用了JSR-250的注解,如@Resource/@postConstruct/@preDestroy,還需要下列jar文件
         * SPRING_HOME/lib/j2ee/common-annotations.jar

    手動添加schema文件,方法如下:
    windows->preferences->myeclipse->files and editors->xml->xmlcatalog
    文件在dist/resources目錄下的spring-beans-2.5.xsd

    2 applicationContext.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi
    ="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context
    ="http://www.springframework.org/schema/context"
           xsi:schemaLocation
    ="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context-2.5.xsd"
    >
        
    <context:annotation-config />//開啟配置項

        
    <bean id="personDao" class="org.spring.dao.impl.PersonDaoImpl"/>
    </beans>
    3 在java代碼中使用@Autowired或@Resource注解方式進行裝配,它們區別是:
      @Autowired默認按類型裝配,@Resource默認按名稱裝配,當找不到與名稱匹配的bean才會按類型裝配
     
      # 用于字段上
      @Autowired
      private PersonDao personDao;
      # 用于屬性的setter方法上
      @Autowired
      public void setPersonDao(PersonDao personDao){...}
             * @Autowired默認按類型裝配,默認情況下要求依賴對象必須存在,如果允許null值,可以設置它required為false,
             如果想使用按名稱裝配,可以結合@Qualifier注解一起使用,如下:
                @Autowired @Qualifier("personDaoBean")
                private PersonDao personDao;
             * @Resource默認按名稱裝配,找不到匹配的bean才會按類型裝配。它和@Autowired一樣可以標注在字段或屬性的setter方法上。
             可以通過name屬性指定名稱,如果沒有指定name屬性,當注解標注在字段上,即默認取字段的名稱為name屬性名稱,當注注解
             標注在屬性的setter方法上,即默認取屬性名作為bean名稱尋找依賴對象。
             注意:一旦指定了name屬性,就只能按名稱裝配了。
                @Resource(name="personDao")
                private PersonDao personDao;
      例(繼上):
    public class PersonBizImpl implements IPersonBiz {
        @Resource[或@Resource(name
    ="personDao")]//指定bean的名稱,和applicationContext.xml文件中bean名稱相同
        private IPersonDao personDao;//可以不用寫setter方法
        
        
    //或者在屬性的setter方法上
        
    //@Resource
        
    //public void setPersonDao(IPersonDao personDao) {
        
    //    this.personDao = personDao;
        
    //}

        
    public void say(){
            personDao.say();
        }

      }

    posted on 2009-11-30 08:58 junly 閱讀(350) 評論(0)  編輯  收藏 所屬分類: spring

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


    網站導航:
     
    主站蜘蛛池模板: 亚洲欧美日韩中文二区| 亚洲高清无在码在线无弹窗 | 亚洲A∨无码一区二区三区| 特级无码毛片免费视频| 国产大片51精品免费观看| 国产成人亚洲综合网站不卡| 成人奭片免费观看| 亚洲色偷精品一区二区三区 | 自拍偷自拍亚洲精品被多人伦好爽 | 免费黄色福利视频| 亚洲av永久无码嘿嘿嘿| 嫩草视频在线免费观看| 最新亚洲春色Av无码专区| 国产免费无遮挡精品视频| 免费一级特黄特色大片| 亚洲Av综合色区无码专区桃色| 无码一区二区三区免费| 亚洲综合久久一本伊伊区| 日韩一区二区免费视频| eeuss在线兵区免费观看| 亚洲国产精品久久久天堂| 18观看免费永久视频| 日韩亚洲国产高清免费视频| 亚洲精品久久久www| 日日麻批免费40分钟无码| 亚洲一区二区三区免费观看| 国产成人高清精品免费软件| 美女网站在线观看视频免费的| 亚洲日本一区二区| 免费无码黄网站在线观看| 两个人看的www免费高清| 亚洲无成人网77777| 日韩精品成人亚洲专区| 久久久久免费精品国产小说| 在线综合亚洲欧洲综合网站| 亚洲中文字幕无码爆乳AV| 国产成人免费高清激情明星| 一级成人a做片免费| 亚洲不卡中文字幕| 国产日产亚洲系列| 97人伦色伦成人免费视频 |