Java_web开发_SSH spring中取得Bean实例的方法

本文转:http://blog.csdn.net/songylwq/archive/2010/07/21/5752383.aspx

 

获得spring里注册Bean的四种方法,特别是第三种方法,简单:
一:方法一(多在struts框架中)继承BaseDispatchAction

 /

import com.mas.wawacommunity.wap.service.UserManager;
 
public class BaseDispatchAction extends DispatchAction {
    /**
    * web应用上下文环境变量
    */
    protected WebApplicationContext ctx;
 
    protected UserManager userMgr;
 
    /**
    * 获得注册Bean     
    * @param beanName String 注册Bean的名称
    * @return
    */
    protected final Object getBean(String beanName) {
        return ctx.getBean(beanName);
    }
 
    protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
              javax.servlet.http.HttpServletRequest request,
              javax.servlet.http.HttpServletResponse response) {     
        return mapping.findForward("index");
    }
 
    public void setServlet(ActionServlet servlet) {
        this.servlet = servlet;
        this.ctx = WebApplicationContextUtils.getWebApplicationContext(servlet.getServletContext());
        this.userMgr = (UserManager) getBean("userManager");
    }         
}

 

二:方法二实现BeanFactoryAware
一定要在spring.xml中加上:
<bean id="serviceLocator" class="项目中ServiceLocator的类路径比如:com.am.oa.commons.service.SpringContextUtil " singleton="true" />
当对serviceLocator实例时就自动设置BeanFactory,以便后来可直接用beanFactory

 

public class ServiceLocator implements BeanFactoryAware {
    private static BeanFactory beanFactory = null;
 
    private static ServiceLocator servlocator = null;
 
    public void setBeanFactory(BeanFactory factory) throws BeansException {
        this.beanFactory = factory;
    }
 
    public BeanFactory getBeanFactory() {
        return beanFactory;
    }
 
   
    public static ServiceLocator getInstance() {
        if (servlocator == null)
              servlocator = (ServiceLocator) beanFactory.getBean("serviceLocator");
        return servlocator;
    }
 
    /**
    * 根据提供的bean名称得到相应的服务类     
    * @param servName bean名称     
    */
    public static Object getService(String servName) {
        return beanFactory.getBean(servName);
    }
 
    /**
    * 根据提供的bean名称得到对应于指定类型的服务类
    * @param servName bean名称
    * @param clazz 返回的bean类型,若类型不匹配,将抛出异常
    */
    public static Object getService(String servName, Class clazz) {
        return beanFactory.getBean(servName, clazz);
    }
}

   action调用: 

 

public class UserAction extends BaseAction implements Action,ModelDriven{
    
    private Users user = new Users();
    protected ServiceLocator service = ServiceLocator.getInstance();
    UserService userService = (UserService)service.getService("userService");
 
    public String execute() throws Exception {         
        return SUCCESS;
    }
 
  public Object getModel() {
        return user;
    }     
    
    public String getAllUser(){
        HttpServletRequest request = ServletActionContext.getRequest();         
        List ls=userService.LoadAllObject( Users.class );
        request.setAttribute("user",ls);     
        this.setUrl("/yonghu.jsp");
        return SUCCESS;
    }
}

 
  

自己使用:

1.如果applicationContext文件是在src的根目录下:

  ApplicationContext apt= new FileSystemXmlApplicationContext("src/applicationContext.xml");

apt.getBean("ID");

可以取得

2.如果文件是在WebRoot/WEB-INF下可以用

ApplicationContext applicationContext= new FileSystemXmlApplicationContext("../applicationContext.xml");

取得

3.如果是在servlet中取可用

  ServletContext application = this.getServletContext();
 
  //获取spring上下文信息 为servlet使用
  WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(application);
  IDicService dicService = (IDicService)wac.getBean("dicService");

 

在servlet或者filter或者Listener中使用spring的IOC容器的方法是:

WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext()); 

由于spring是注入的对象放在ServletContext中的,所以可以直接在ServletContext取出WebApplicationContext 对象:

WebApplicationContext webApplicationContext = (WebApplicationContext) servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

事实上WebApplicationContextUtils.getWebApplicationContext方法就是使用上面的代码实现的,建议使用上面上面的静态方法 


注意:在使用webApplicationContext.getBean("ServiceName")的时候,前面强制转化要使用接口,如果使用实现类会报类型转换错误。如:
LUserService userService = (LUserService) webApplicationContext.getBean("userService");  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值