【发布时间】: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