Spring web应用下怎么方便的获得bean?

如果我们的系统不是分布式的(在分布式里,我一般自己加载spring的配置文件),不是一般的application,通过自己加载Spring的配置文件的方式。而是一般的web应用,我们通过在web.xml里配置spring的配置文件。我们怎么方便的得到一个Bean的实例呢?当然,web应用启动后,它已经创建好一个WebApplicationContext(这个是接口,其实也是ApplicationContext类型的,因为WebApplicationContext继承自ApplictionContext这个接口)类型的实例对象,通过org.springframework.web.context.support.WebApplicationContextUtils里的
getWebApplicationContext(ServletContext sc)可以得到这个对象的引用(这个就像我们一般的java application下得到ApplicationContext类型的引用一样),我们就可以通过它的getBean方法得到我们的bean实例了。但是这里有个问题getWebApplicationContext(ServletContext sc)这个方法的参数ServletContext代表的是你web应用的环境,也就是说,也就是说web应用环境下特有的。这个时候如果你想得到一个bean的话,必须要有这个ServletContext对象存在,如果你每个类里都写一个方法来接受ServletContext对象,从而得到WebApplicationContext类型实例的引用,之后再得到bean,进行你要的操作,这个是不是很麻烦?这个不是要写很多代码么?我觉得可以把获得bean的这个操作的功能代码放在一个Servlet里,让这个Servlet在web应用启动的时候加载,我们之后把这个Servlet当作普通类使用,调用里面的getBean方法就可以了。这个servlet的代码如下:

 

package  jimmee.cn.edu.zju.pdm.framework.server;


import  javax.servlet.ServletException;
import  javax.servlet.http.HttpServlet;

import  org.springframework.web.context.WebApplicationContext;
import  org.springframework.web.context.support.WebApplicationContextUtils;

public   class  GetBeanServlet  extends  HttpServlet
{

    
private static WebApplicationContext context;
    
    
public void init() throws ServletException
    
{
        context
=WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
    }

    
    
public static Object getBean(String id)
    
{
        Object bean 
= context.getBean(id);
        
        
return bean;
    }

}


.Spring在web应用

 

<? xml version = " 1.0 "  encoding = " UTF-8 " ?>
< web - app version = " 2.5 "  
    xmlns
= " http://java.sun.com/xml/ns/javaee "  
    xmlns:xsi
= " http://www.w3.org/2001/XMLSchema-instance "  
    xsi:schemaLocation
= " http://java.sun.com/xml/ns/javaee 
    http: // java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <!--  Spring的配置  -->
  
< context - param >
        
< param - name > contextConfigLocation </ param - name >
        
< param - value >/ WEB - INF / beans.xml </ param - value >
    
</ context - param >
    
< context - param >
        
< param - name > log4jConfigLocation </ param - name >
        
< param - value >/ WEB - INF / log4j.properties </ param - value >
    
</ context - param >
    
< servlet >
        
< servlet - name > springInitServlet </ servlet - name >
        
< servlet - class >
            org.springframework.web.context.ContextLoaderServlet
        
</ servlet - class >
        
< load - on - startup > 1 </ load - on - startup >
    
</ servlet >
    
< servlet >
        
< servlet - name > log4jInitServlet </ servlet - name >
        
< servlet - class >
            org.springframework.web.util.Log4jConfigServlet
        
</ servlet - class >
        
< load - on - startup > 2 </ load - on - startup >
    
</ servlet >
  
< servlet >
    
< servlet - name > GetBeanServlet </ servlet - name >
    
< servlet - class > jimmee.cn.edu.zju.pdm.framework.server.GetBeanServlet </ servlet - class >
      
< load - on - startup > 3 </ load - on - startup >
  
</ servlet >
   
<!-- Spring配置结束   -->
  
< servlet - mapping >
    
< servlet - name > GetBeanServlet </ servlet - name >
    
< url - pattern >/ servlet / GetBeanServlet </ url - pattern >
  
</ servlet - mapping >
 
  
< welcome - file - list >
    
< welcome - file > index.jsp </ welcome - file >
  
</ welcome - file - list >
</ web - app >

 

使用示例:

假使我有一个Person的类,属性有name和age,在spring配置文件里配置的id为“person”

你在你的servlet或者jsp想得到这个实例的时候,直接这么做就可以了:

Person person=(Person)GetBeanServlet.getBean("person");

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值