在 Spring 自动装配 模式下手工从IOC中获取实例

  在项目开发时,如果采用了Spring的自动装配时,有时我们在封装自定义JSP标签时,会用到服务层的方法,这是我们需要通过Spring IOC来获取实例,这时我们可以获取Spring IOC中一个比较关键的对象:ApplicationContext

  代码如下:

  public static Object getBean(String name){
        ApplicationContext conn=(ApplicationContext) ServletActionContext.getServletContext().getAttribute(WebApplicationContext.class.getName() + ".ROOT");
       return conn.getBean(name);
    }

 

 

posted on 2012-08-22 19:56  上善若水123  阅读(431)  评论(0编辑  收藏  举报