Spring-Version: 2.0.2
tomcat 6我用的如下代码:
public class x extends SimpleTagSupport{
        ...
@Override
public void doTag() throws JspException, IOException {
ServletContext servletContext = ((PageContext) this.getJspContext()).getServletContext();
WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        ExpenseItemService expenseItemService = (ExpenseItemService) wac.getBean("expenseItemService",ExpenseItemService.class);    //在这行抛java.lang.NullPointerException
                ...
        }}

解决方案 »

  1.   

    整理下代码:public class x extends SimpleTagSupport{
            //etc...
            @Override
            public void doTag() throws JspException, IOException {
                        ServletContext servletContext = ((PageContext) this.getJspContext()).getServletContext();
                        WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(servletContext);
                        ExpenseItemService expenseItemService = (ExpenseItemService) wac.getBean("expenseItemService",ExpenseItemService.class);    //在这行抛java.lang.NullPointerException
                        //etc...
            }} 
      

  2.   

    也许你需要自己保存一个静态的SpringContext对象,然后从这个对象中获取你需要的bean
      

  3.   

     WebApplicationContextUtils.getWebApplicationContext(servletContext);去调试这个方法,返回为null
      

  4.   

    WebApplicationContext wac = WebApplicationContextUtils.  getRequiredWebApplicationContext(servletContext); 
      

  5.   

    兄弟!这两个方法如出一撤!
    getWebApplicationContext=null
    getRequiredWebApplicationContext=IllegalStateException Exception