2008-04-18

運用Spring 手動取bean的方法

关键字: 手動取bean
大家知道,儅你用Spring管理bean時,在自己定義的過濾器中是不能通過Spring取到bean的,必須自己手動取,現將我笨拙的方法寫下來供大家參考

首先在web.xml中加入如下代碼
	<servlet>
		<servlet-name>GetSpringContext</servlet-name>
		<servlet-class>com.morris.GetSpringContext</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>


具體實現如下:
public class GetSpringContext extends HttpServlet {

	/**
	 * @author Morris
	 */
	private static final long serialVersionUID = 1L;
	private static Log logger = LogFactory.getLog(GetSpringContext.class);
	public static commonService commonService = null;
	
	public GetSpringContext(){};
	
    public void init(){
        WebApplicationContext ctx =WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        commonService = (commonService)ctx.getBean("commonService"); //自己隨便取啦
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) {

    }
评论
zhangxi123 2008-06-27
[color=red][/color]
bulargy 2008-05-19
确实该投隐藏~~~
movingboy 2008-04-18
竟然被投隐藏啊???这也太打击人了吧
尽管内容比较简单,但楼主的奉献精神还是要肯定的。我投个新手好了
kakaluyi 2008-04-18
不说了你这贴估计用不了多久
会被集体暴力。。。
发表评论

提醒: 该博客已发表在公共论坛,博客所有留言会成为论坛回贴,留言请注意遵守论坛发贴规则

您还没有登录,请登录后发表评论

morris
搜索本博客
存档
最新评论