<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 有貓相伴的日子 閱讀(1709) 評論(0)  編輯  收藏 所屬分類: spring
    本站不再更新,歡迎光臨 java開發技術網
    主站蜘蛛池模板: 18禁男女爽爽爽午夜网站免费| 成人免费视频试看120秒| 亚洲精品成人网站在线播放| 免费下载成人电影| 人妻巨大乳hd免费看| 亚洲精品高清久久| 卡1卡2卡3卡4卡5免费视频| 国产免费久久久久久无码| 亚洲精品午夜久久久伊人| 免费成人av电影| 麻花传媒剧在线mv免费观看 | 麻豆亚洲AV永久无码精品久久| 久久综合AV免费观看| 羞羞视频免费网站在线看| 亚洲a视频在线观看| 亚洲熟妇丰满多毛XXXX| 免费三级毛片电影片| a毛片久久免费观看| 欧洲亚洲国产精华液| 久久99亚洲网美利坚合众国| 四虎影视永久免费观看网址| 在线观看永久免费| 免费毛片在线看不用播放器| 精品久久久久久久久亚洲偷窥女厕| 亚洲AV第一页国产精品| 亚洲国产综合精品中文字幕 | 国产成人久久AV免费| 免费国产在线精品一区| 国产成人精品日本亚洲18图| 国产亚洲一区二区在线观看| 免费看一级做a爰片久久| 美女裸身网站免费看免费网站| 野花香在线视频免费观看大全| 国产亚洲综合视频| 亚洲色偷偷偷综合网| 亚洲视频免费播放| 亚洲一区二区影院| 亚洲AV无码一区二区三区系列| 亚洲国产黄在线观看| 国产大片线上免费看| 在线jyzzjyzz免费视频|