<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 李嵐 閱讀(922) 評論(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永久| 91香蕉视频免费| 成人亚洲国产va天堂| 波多野结衣久久高清免费| 午夜亚洲国产理论片二级港台二级| 午夜两性色视频免费网站| 亚洲av永久无码精品秋霞电影秋 | 亚洲日本韩国在线| 亚欧乱色国产精品免费视频| 亚洲无线码一区二区三区| 中文字幕久精品免费视频| 91亚洲自偷手机在线观看| 成人免费视频77777| 亚洲爆乳少妇无码激情| 无码专区一va亚洲v专区在线 | 久久久久国色AV免费观看| 亚洲国产精品无码久久久秋霞2| 无码精品国产一区二区三区免费 | 波多野结衣免费一区视频| 久久精品亚洲日本佐佐木明希| 222www免费视频| 亚洲熟妇AV一区二区三区宅男| 亚洲国产成人久久综合区| 免费毛片在线看不用播放器| 亚洲熟妇av一区二区三区下载| 午夜免费福利在线| 国产在线国偷精品免费看| 亚洲成人黄色网址| 免费在线观看中文字幕| 久久国产精品免费网站| 亚洲综合色丁香婷婷六月图片| 亚洲片国产一区一级在线观看| 午夜精品免费在线观看 | 亚洲中文字幕人成乱码 | 免费观看黄色的网站| 特级毛片全部免费播放a一级| 久久精品亚洲一区二区|