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

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

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

    本站不再更新,歡迎光臨 java開發技術網
    隨筆-230  評論-230  文章-8  trackbacks-0
    Spring 從核心而言是一個DI(依懶注入)容器,其設計是一種無入侵性的高擴展框架,即無需代碼中涉及Spring專有類,即可將其納入Spring容易進行管理。org.springframework.beans包中包括了這些核心組件的實現類。核心中的核心為BeanWrapper和BeanFactory類,從技術角度來說這兩個類并不復雜。

    1、Bean Wrapper
    ?????? 這個BeanWrapper類很簡單,提供了一套Bean的操作方法,如apache -BeanUtils一樣。通過BeanWrapper,我們可以無需在編碼時就指定JavaBean的實現類和屬性值,通過在配置文件
    加以設定,就可以在運行期動態創建對象并設定其屬性(依賴關系)。 一個簡單的例子:
    ????public?static?void?testBeanWrapper(){
    ????????
    try?{
    ????????????Object?obj
    =Class.forName("com.spring.UpperAction").newInstance();
    ????????????BeanWrapper?bw
    =new?BeanWrapperImpl(obj);

    ????????????bw.setPropertyValue(
    "message","peidw");
    ????????????System.out.println(bw.getPropertyValue(
    "message"));
    ????????}?
    catch?(InstantiationException?e)?{
    ????????????
    //?TODO?Auto-generated?catch?block
    ????????????e.printStackTrace();
    ????????}?
    catch?(IllegalAccessException?e)?{
    ????????????
    //?TODO?Auto-generated?catch?block
    ????????????e.printStackTrace();
    ????????}?
    catch?(ClassNotFoundException?e)?{
    ????????????
    //?TODO?Auto-generated?catch?block
    ????????????e.printStackTrace();
    ????????}
    ????????
    ????}

    2、Bean Factory
    ????? BeanFactory負責根據配置配置文件創建Bean實例,可以配置的項目有:
    ????? 1 、Bean屬性值及依賴關系
    ????? 2、Bean創建模式(是否單態模式)
    ????? 3、Bean初始化及銷毀
    ????? 4、Bean的依賴關系
    <beans>
    <description>Spring?Bean?Configuration?Sample</description>
    <bean
    id
    ="TheAction"?⑴
    class="net.xiaxin.spring.qs.UpperAction"?⑵
    singleton
    ="true"?⑶
    init
    -method="init"?⑷
    destroy
    -method="cleanup"?⑸
    depends
    -on="ActionManager"?⑹
    >
    <property?name="message">
    <value>HeLLo</value>?⑺
    </property>
    <property?name="desc">
    <null/>
    </property>
    <property?name="dataSource">
    <ref?local="dataSource"/>?⑻
    </property>
    </bean>
    <bean?id="dataSource"
    class="org.springframework.jndi.JndiObjectFactoryBean">
    <property?name="jndiName">
    <value>java:comp/env/jdbc/sample</value>
    </property>
    </bean>
    這是一個比較完整的Bean配置,id表示一個類在BeanFactory中的唯一標識
    class 是java類名,singleton 為true表示使用單態,init-method初始化方法在BeanFactory創建Bean后執行的初始化方法。destroy-method銷毀方法,在bean銷毀時執行,一般用于資源釋放。depends-on? Bean的依懶關系。<value>節點用于指定一個屬性值,ref節點用于引用另一個bean的屬性。

    ?????? 聯合上面關于BeanWrapper的內容,我們可以看到,BeanWrapper實現了針對單個Bean的屬性設定操作。而BeanFactory則是針對多個Bean的管理容器,根據給定的配置文件,BeanFactory從中讀取類名、屬性名/值,然后通過Reflection機制進行Bean加載和屬性設定。

    3、ApplicationContext
    BeanFactory提供了針對Java Bean的管理功能,而ApplicationContext提供了一個更為框架化的
    實現(從上面的示例中可以看出,BeanFactory的使用方式更加類似一個API,而非Framework style)。
    ApplicationContext覆蓋了BeanFactory的所有功能,并提供了更多的特性。此外,
    ApplicationContext為與現有應用框架相整合,提供了更為開放式的實現(如對于Web應用,我們可以在
    web.xml中對ApplicationContext進行配置)。
    相對BeanFactory而言,ApplicationContext提供了以下擴展功能:
    1. 國際化支持
    我們可以在Beans.xml文件中,對程序中的語言信息(如提示信息)進行定義,將程序中的提示
    信息抽取到配置文件中加以定義,為我們進行應用的各語言版本轉換提供了極大的靈活性。
    2. 資源訪問
    支持對文件和URL的訪問。
    3. 事件傳播
    事件傳播特性為系統中狀態改變時的檢測提供了良好支持。
    4. 多實例加載
    可以在同一個應用中加載多個Context實例。

    一個國際化支持例子
    public?static?void?testLanguage(){
    ????????ApplicationContext?ac
    =new?FileSystemXmlApplicationContext("src/bean.xml");
    ????????Object[]?arg
    =new?Object[]{"kkui",Calendar.getInstance().getTime()};
    ????????
    //以系統默認的Locale加載信息(對winxp而言默認的是zh_CN)

    ????????String?zhmsg
    =ac.getMessage("userinfo",arg,?Locale.CHINA);
    ????????String?usmsg
    =ac.getMessage("userinfo",arg,?Locale.US);
    ????????System.out.println(zhmsg);
    ????????System.out.println(usmsg);
    ????}
    需要在beans.xml文件配置使用多資源
    ????<bean?id="messageSource"?class="org.springframework.context.support.ResourceBundleMessageSource">
    ????????
    <property?name="basenames">
    ????????????
    <list>
    ????????????????
    <value>messages</value>
    ????????????
    </list>
    ????????
    </property>
    ????
    </bean>
    這里聲明了一個名為messageSource的Bean(注意對于Message定義,Bean ID必須為
    messageSource,這是目前Spring的編碼規約),對應類為ResourceBundleMessageSource,
    目前Spring中提供了兩個MessageSource接口的實現,即
    ResourceBundleMessageSource和ReloadableResourceBundleMessageSource,后
    者提供了無需重啟即可重新加載配置信息的特性。
    資源文件message_zh.properties
    userinfo=登陸用戶:[{0}]?登陸時間[{1}]

    國際化支持在實際開發中可能是最常用的特性。對于一個需要支持不同語言環境的應用而言,我們所采取的最常用的策略一般是通過一個獨立的資源文件(如一個properties文件)完成所有語言信息(如界面上的提示信息)的配置,Spring對這種傳統的方式進行了封裝,并提供了更加強大的功能.

    資源訪問例子

    Resource?rs?=?ctx.getResource("classpath:config.properties");
    File?file?=?rs.getFile();
    事件傳播機制例子
    ApplicationContext基于Observer模式(java.util包中有對應實現),提供了針對Bean的事件傳播功能。通過Application. publishEvent方法,我們可以將事件通知系統內所有的ApplicationListener。下面是一個例子:
    package?com.spring;


    import?org.springframework.beans.BeansException;
    import?org.springframework.context.ApplicationContext;
    import?org.springframework.context.ApplicationContextAware;
    import?org.springframework.context.ApplicationEvent;

    public?class?LoginAction?implements?ApplicationContextAware{
    ????
    private?ApplicationContext?ac;
    ????
    public?void?setApplicationContext(ApplicationContext?arg0)?throws?BeansException?{
    ????????
    //?TODO?Auto-generated?method?stub
    ????????this.ac=arg0;
    ????}
    ????
    public?int?login(String?username,String?password){
    ????????ActionEvent?ae
    =new?ActionEvent(username);
    ????????
    this.ac.publishEvent(ae);
    ????????
    return?0;
    ????}
    ????
    }
    package?com.spring;

    import?org.springframework.context.ApplicationEvent;

    public?class?ActionEvent?extends?ApplicationEvent?{
    ????
    public?ActionEvent(Object?source)?{
    ????????
    super(source);
    ????}
    }
    package?com.spring;

    import?org.springframework.context.ApplicationEvent;
    import?org.springframework.context.ApplicationListener;

    public?class?ActionListener?implements?ApplicationListener{

    ????
    public?void?onApplicationEvent(ApplicationEvent?event)?{
    ????????
    if(event?instanceof?ActionEvent){
    ????????????System.out.println(event.toString());
    ????????}
    ????????
    ????}
    ????
    }
    需要在beans.xml文件里添加以下配置
    <bean?id="loginaction"?class="net.xiaxin.beans.LoginAction"/>
    <bean?id
    ="listener"?class="net.xiaxin.beans.ActionListener"/>
    測試方法:
    ????public?static?void?testListener(){
    ????????ApplicationContext?ac
    =new?FileSystemXmlApplicationContext("src/bean.xml");
    ????????LoginAction?la
    =(LoginAction)ac.getBean("loginaction");
    ????????la.login(
    "peidw",?"123456");
    ????}
    Struts in Spring
    Struts和Spring是如何整合,為了在struts加加載Spring Context;在struts-config.xml文件中添加以下配置
    <struts-config>
    <plug-in
    className="org.springframework.web.struts.ContextLoaderPlugIn">
    <set-property?property="contextConfigLocation"
    value
    ="/WEB-INF/applicationContext.xml"?/>
    </plug-in>
    </struts-config>
    通過plug-in我們實現了Spring Context的加載,不過僅僅加載Context并沒有什么實際意義,我們還需要修改配置,將Struts Action交給Spring容器進行管理:
    posted on 2006-11-25 20:30 有貓相伴的日子 閱讀(1708) 評論(0)  編輯  收藏 所屬分類: spring
    本站不再更新,歡迎光臨 java開發技術網
    主站蜘蛛池模板: 亚洲AV综合色区无码一区爱AV | 久久国产一片免费观看| 亚洲精品无码MV在线观看| 久久久久久毛片免费播放| 韩国亚洲伊人久久综合影院| 亚洲国产天堂久久久久久| 久久久久久久久久国产精品免费| 亚洲色偷偷综合亚洲AV伊人蜜桃 | 激情综合亚洲色婷婷五月| 亚洲精品视频免费| 午夜国产精品免费观看| 久久久久女教师免费一区| 亚洲一级视频在线观看| 亚洲一区二区三区影院 | 亚洲精品中文字幕无码蜜桃 | 亚洲AV无码专区国产乱码4SE | 大学生美女毛片免费视频| 欧洲人免费视频网站在线| 亚洲乱人伦中文字幕无码| 久久国产精品亚洲综合 | 亚洲性猛交xx乱| 国产偷国产偷亚洲清高动态图| 男人的好免费观看在线视频| 中国国语毛片免费观看视频| 亚洲国产精品无码久久九九大片| 亚洲麻豆精品果冻传媒| 亚洲五月综合缴情在线观看| 国产成人高清精品免费鸭子| 丁香花免费完整高清观看 | 亚洲综合色婷婷七月丁香| 日本一线a视频免费观看| jjizz全部免费看片| a级毛片在线视频免费观看| 情人伊人久久综合亚洲| 四虎永久在线免费观看| 成年性生交大片免费看| 91精品全国免费观看含羞草| 免费看一区二区三区四区| 国产精品美女久久久免费| 美女羞羞视频免费网站| 亚洲av无码成人精品区一本二本|