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

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

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

    Spring和Webwork結合的方式

    Posted on 2005-11-23 13:52 李嵐 閱讀(916) 評論(0)  編輯  收藏 所屬分類: Java

    前兩種方式webwork的wiki里有,就不多說了,點這里可以查看。
    而以下的給出的思路是webwork mail list的討論的一種方案,應該是最好的一種方案。

    代碼1:

    public class SpringContainer implements Container {
        
    private
     ApplicationContext applicationContext;

        
    public SpringContainer(ServletContext servletContext) 
    {
            
    this.applicationContext =
     WebApplicationContextUtils.getWebApplicationContext(servletContext);
        }


        
    public SpringContainer(ApplicationContext applicationContext) {
            
    this.applicationContext =
     applicationContext;
        }


        
    /**
         * 
    @param key
         *            component class type or component name
         * 
    @return @throws

         *         ComponentNotFoundException
         
    */

        
    public Object getComponent(Object key) throws ComponentNotFoundException {
            
    if (applicationContext == null
    )
                
    throw new IllegalStateException("Spring Application context has not been set"
    );
            
    if (key == null
    )
                
    throw new ComponentNotFoundException("The component key can not be null"
    );
            
    if (key instanceof Class) 
    {
                Map beans 
    =
     applicationContext.getBeansOfType((Class) key);
                
    if (beans == null
    )
                    
    throw new ComponentNotFoundException("The container is unable to resolve single instance of " +
     ((Class) key).getName()
                            
    + ", none instances found"
    );
                
    if (beans.size() == 0 || beans.size() > 1
    )
                    
    throw new ComponentNotFoundException("The container is unable to resolve single instance of " +
     ((Class) key).getName()
                            
    + ", number of instances found was: " +
     beans.size());
                key 
    =
     beans.keySet().iterator().next();
            }

            
    return applicationContext.getBean(key.toString());
        }


        
    public void reload() {
            close();
            ((AbstractApplicationContext) applicationContext).refresh();
        }


        
    public void autowireComponent(Object bean) {
            ((AbstractApplicationContext) applicationContext).getBeanFactory().autowireBeanProperties(bean, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, 
    false
    );
        }


        
    public void close() {
            ((AbstractApplicationContext) applicationContext).close();
        }


    }

    代碼2:
    public class ComponentAutowireInterceptor implements Interceptor {


        
    public void destroy() 
    {
            
    // TODO Auto-generated method stub

        }


        
    public void init() {
            
    // TODO Auto-generated method stub

        }


        
    public String intercept(ActionInvocation invocation) throws Exception {
            Application.getInstance().getContainer().autowireComponent(invocation.getAction());
            
    return
     invocation.invoke();
        }


    }


    這幾段代碼,你不需要在spring里配置action,也不需要在xwork.xml里面配置external-ref。只要保證action引用的東東和spring里面同名就能夠auto wire by name了。

    原理:攔截器會在每個action之前調用autowireComponent(invocation.getAction())。這樣spring就會去自動組裝這個傳進來的action對象,而action對象里的屬性和spring里的bean id同名。所以spring就會自動將application.xml里對應的bean注射到action的屬性里去,從而達到把action按用戶希望的方式組裝起來的目的。

    如:
    appliaction.xml
        <bean id="myService">
            
    <property name="target">
                
    <bean class="myServiceDefaultImpl" autowire="byName">
              
    </property>
        
    </bean>

    xwork.xml
            <action name="myAction" class="myAction">
                
    <result name="success">/success.ftl</result>
            
    </action>

    Action:
    public class myAction extends ActionSupport {
        protected myService;
             .......

        }

    最后記得要在web.xml配上listener-class

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


    網站導航:
     

    posts - 7, comments - 23, trackbacks - 0, articles - 0

    Copyright © 李嵐

    主站蜘蛛池模板: 亚洲AV无码AV日韩AV网站| 99精品免费视品| 337P日本欧洲亚洲大胆艺术图| 蜜桃传媒一区二区亚洲AV | 亚洲sm另类一区二区三区| 免费在线黄色电影| 在线观看特色大片免费视频| 尤物永久免费AV无码网站| 久久精品九九亚洲精品天堂 | 亚洲国产成人精品女人久久久| 久久久青草青青亚洲国产免观| 亚洲一区二区在线免费观看| 日本亚洲色大成网站www久久 | 国产在线a不卡免费视频| 亚洲精品无码久久毛片波多野吉衣| 亚洲av乱码中文一区二区三区| 国产国产人免费人成成免视频| 波多野结衣免费在线观看| 色久悠悠婷婷综合在线亚洲| 亚洲免费观看网站| fc2免费人成在线| 成人人观看的免费毛片| 一区二区三区亚洲| 最近中文字幕大全中文字幕免费| 亚洲人成电影在线播放| 亚洲熟妇自偷自拍另欧美| 99精品视频在线观看免费专区| avtt亚洲天堂| 农村寡妇一级毛片免费看视频| 精品久久免费视频| 亚洲人成网站免费播放| 毛片免费观看网站| 亚洲午夜在线播放| 午夜毛片不卡免费观看视频| 亚洲av无码专区在线观看亚| 国产男女猛烈无遮挡免费视频网站 | 在线看片免费人成视频福利| 伊人久久综在合线亚洲91| 国产免费播放一区二区| 国产亚洲av片在线观看18女人 | 亚洲AV无码国产一区二区三区|