webwork2.1.5使用spring的ioc容器方法

webwork2.1.5使用spring的ioc容器方法:

首先创建几个类:
Application.java,Container.java,SpringContainer.java,DefaultApplicationSetupListener.java,ComponentNotFoundException.java
其中Container.java为接口,SpringContainer.java实现Container.java接口
代码如下:

Application:
package util;

/**
 * @author Davin
 * @since 2007-12-23
 * @version $Revision: 1.1 $
 */
public class Application {
    private Container container;
    private static Application instance;
   
    private Application(){
       
    }
   
    public static Application getInstance() {
        if (instance == null)
            instance = new Application();
        return instance;
    }
   
    public Container getContainer() {
        return container;
    }
    public void setContainer(Container container) {
        this.container = container;
    }
}


Container:
package util;

/**
 * @author Davin
 * @since 2007-12-23
 * @version $Revision: 1.1 $
 */
public interface Container {
    public Object getComponent(Object key) throws ComponentNotFoundException; 

    public void reload();
   
    public void autowireComponent(Object bean);
   
    public void close();
}

 

SpringContainer:

package util;

import java.util.Locale;

import javax.servlet.ServletContext;

import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * @author Davin
 * @since 2007-12-23
 * @version $Revision: 1.2 $
 */
public class SpringContainer implements Container {
    private static 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) {
            String names[] = applicationContext.getBeanNamesForType((Class) key);
            if (names == null)
                throw new ComponentNotFoundException("The container is unable to resolve single instance of " + ((Class) key).getName()
                        + ", none instances found");
            if (names.length == 0 || names.length > 1)
                throw new ComponentNotFoundException("The container is unable to resolve single instance of " + ((Class) key).getName()
                        + ", number of instances found was: " + names.length);
            key = names[0];
        }
        return applicationContext.getBean(key.toString());
    }
    public static Object getBean(String beanName) {
        return applicationContext.getBean(beanName);
    }

    public static String getMessage(String messageName) {
        String msg = applicationContext.getMessage(messageName, null, Locale.CHINA);
        return msg;
    }
    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();
    }

}

ComponentNotFoundException:

package util;
/**
 * @author Davin
 * @since 2007-12-23
 * @version $Revision: 1.1 $
 */
public class ComponentNotFoundException extends RuntimeException {

    public ComponentNotFoundException(String message) {
        super(message);
    }

    public ComponentNotFoundException(String message, Throwable cause) {
        super(message, cause);
    }
}


DefaultApplicationSetupListener:
package listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import util.Application;
import util.SpringContainer;


/**
 * DefaultApplicationSetupListener that sets up the environment so that
 * XWork and Webwork can load data and information from Spring. Relies on
 * Spring's {@link org.springframework.web.context.ContextLoaderListener}having
 * been called first.
 *
 * @author Davin
 * @since 2007-12-22
 * @version $Revision: 1.1 $
 */
public class DefaultApplicationSetupListener implements ServletContextListener {

    public void contextInitialized(ServletContextEvent event) {
        SpringContainer container = new SpringContainer(event.getServletContext());
        Application.getInstance().setContainer(container);
    }

    public void contextDestroyed(ServletContextEvent arg0) {
        // do nothing
    }

}

但把代码写完了以后,就需要修改配置文件了
首先是web.xml添加:
 <listener>
  <listener-class>
   listener.DefaultApplicationSetupListener
  </listener-class>
 </listener>
必须再spring的监听下面。
其次是修改webwork-default.xml
<interceptor name="component-autowire" class="interceptor.ComponentAutowireInterceptor"/>
并将该监听器加入到默认监听范围。


以上配置好了以后,就可以写一个简单的service类
package service.impl;

import java.util.List;

import service.LoginService;
import dao.AbstractService;
import domain.Admin;

public class LoginServiceImpl extends AbstractService implements LoginService{
 public Admin findAdmin(String userName) {
  List<Admin> list=this.getHibernateTemplate().find("from Admin a where a.userName='"+userName+"'");
  if(list.size()>0)
   return list.get(0);
  else
   return null;
 } 
}
在spring配置文件中:
<bean id="loginService" parent="baseTxProxy" singleton="false">
  <property name="target">
   <bean class="service.impl.LoginServiceImpl" autowire="byName"/>
  </property>
 </bean> 

写一个action类:
package action;

 

import service.LoginService;
import com.opensymphony.xwork.ActionSupport;

public class BaseAction extends ActionSupport {

 private LoginService loginService;

 public LoginService getLoginService() {
  return loginService;
 }

 public void setLoginService(LoginService loginService) {
  this.loginService = loginService;
 }

 public String execute() throws Exception {
  return SUCCESS;
 }
}

xwork.xml文件
<action name="index" class="action.BaseAction">
   <result name="success"  type="freemarker">/login/login.ftl</result>
</action>
这样就可以使用了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值