Spring和Webwork结合的方式

Posted on 2005-11-23 13:52 李岚 阅读(903) 评论(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 © 李岚