严重:servlet appServlet的Servlet.service()引发了异常

时间:2016-05-02 18:32:24

标签: java spring-mvc autowired

我有一个注释为@Repository

的DBUtil类
@Repository
public class DBUtil {

    private SessionFactory sessionFactory;

    public DBUtil( SessionFactory sf ) {
        this.sessionFactory = sf;
    }

    @Transactional
    public Object saveTable( Object obj ){
        Session session = this.sessionFactory.getCurrentSession();
        session.save(obj);
        return obj;
    }
}

在我的servlet-context中,我已经为base-package

创建了条目

现在我正在尝试将“DBUtil”自动装入CommonUtil类,如下所示:

public class CommonUtil {

    @Autowired  
    private DBUtil dbUtil;

    public void setDBUtil( DBUtil dbUtil ){
        this.dbUtil = dbUtil;
    }



    public Member getMemberFromUserName( String userName ){
        String userCheckQry = "from Users where userName like :username";
        Map<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("username", userName);       
        List userList = this.dbUtil.executeHQLQuery(userCheckQry, parameters, false);

        Users user = (Users) userList.get(0);

        String memberQry = "from Member where memberId = :memId";
        Map<String, Object> memParams = new HashMap<String, Object>();
        memParams.put("memId", user.getId() );

        List memList = this.dbUtil.executeHQLQuery( memberQry, memParams, false);
        return (Member) memList.get(0);
    }

}

当我这样做时,我收到以下错误:

May 02, 2016 11:54:36 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet appServlet threw exception
java.lang.NullPointerException
    at com.rasoirecipes.util.CommonUtil.getMemberFromUserName(CommonUtil.java:56)
    at com.rasoirecipes.impl.AuthenticationSuccessHandlerImpl.onAuthenticationSuccess(AuthenticationSuccessHandlerImpl.java:32)
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.successfulAuthentication(AbstractAuthenticationProcessingFilter.java:331)
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:245)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:120)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:64)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:91)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:213)
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:176)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardH

添加类AuthenticationSuccessHandlerImpl :(一旦用户登录,我想将userid和第一个名称存储到会话中)

public class AuthenticationSuccessHandlerImpl implements AuthenticationSuccessHandler {

    private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        //do what you want with 
        System.out.println("came after login "+ request.getPathInfo());
        HttpSession session = request.getSession();
        System.out.println(session);
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        System.out.println(auth.getName());
        CommonUtil common = new CommonUtil();
        Member member = common.getMemberFromUserName(auth.getName());
        System.out.println(member.getName());
        session.setAttribute( "uid", member.getMemberId() );
        session.setAttribute( "uname", member.getName() );

//        SavedRequest savedRequest = new HttpSessionRequestCache().getRequest(request, response);
//        System.out.println(savedRequest.getRedirectUrl());
//        if(savedRequest != null) {
//            System.out.println(savedRequest.getRedirectUrl());
//        }
        String targetUrl = "/"; 
        redirectStrategy.sendRedirect(request, response, targetUrl);
    }

}

1 个答案:

答案 0 :(得分:0)

我相信你的DBUtil类需要注释你的SessionFactory,以便Spring知道如何连接它。

.babelrc

现在看来,没有默认的构造函数,而且(我们可以看到)是调用setter来设置sessionFactory。