【问题标题】:Failed startup of context com.google.appengine.tools.development.DevAppEngineWebAppContext上下文 com.google.appengine.tools.development.DevAppEngineWebAppContext 启动失败
【发布时间】:2015-04-27 07:51:43
【问题描述】:

我正在使用 Spring MVC、google app engine、admin sdk、cloud sql。 我想将 (preferncesDao) dao 类访问到过滤器中。 下面是我的过滤器

public class NameSpaceGoogleSecurityFilter implements Filter
{
    @Autowired
    IPreferencesDao preferncesDao;

    public void init( FilterConfig filterConfig ) throws ServletException{
          SpringUtils.init(filterConfig.getServletContext());
          preferncesDao = SpringUtils.getPreferncesDao();
   }
}

下面是我的 SpringUtils 类。

public class SpringUtils {

   private static ApplicationContext appContext;

   private static IPreferencesDao preferncesDao = null;

   public static void init(final ServletConfig config) {
       init(config.getServletContext());
   }
   public static void init(final ServletContext context) {
      if(appContext==null){
        appContext =
            (ApplicationContext) context.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
      }
   }

   public static IPreferencesDao getPreferncesDao() {
      if(preferncesDao==null){
        preferncesDao=(IPreferencesDao) appContext.getBean("preferncesDao");
      }
     return preferncesDao;
   }

   protected SpringUtils() {
        throw new UnsupportedOperationException();
   }
}

当我开始构建过程时,它会抛出异常

Failed startup of context com.google.appengine.tools.development.DevAppEngineWebAppContext
  java.lang.NullPointerException.  
  Nullpointer at line  preferncesDao=(IPreferencesDao) appContext.getBean("preferncesDao");

如何解决上述错误?将 dao 对象放入过滤器是否正确?如果不是正确的方法是什么?

【问题讨论】:

    标签: spring google-app-engine spring-mvc servlet-filters


    【解决方案1】:

    需要在web.xml中添加下面的标签

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    

    【讨论】:

      【解决方案2】:

      这纯粹表明缺少 ContextLoaderListener。

      所以在 web.xml 中添加以下代码

      <context-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>applicationContext.xml</param-value>
      </context-param>
      
        <listener>
          <listener-class>
                org.springframework.web.context.ContextLoaderListener
          </listener-class>
        </listener>
      

      更多详情请咨询this link

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-09-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-15
        • 1970-01-01
        • 1970-01-01
        • 2020-09-26
        相关资源
        最近更新 更多