0

I have a DBUtil class annotated as @Repository

@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;
    }
}

In my servlet-context i have made the entry for the base-package

Now i am trying to autowire "DBUtil" into CommonUtil class which is as below:

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);
    }

}

When I do this, I get the following error:

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

Adding the class AuthenticationSuccessHandlerImpl : ( As soon as the user logs in, i want to store the userid and the first name into the session)

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);
    }

}
Swaprks
  • 1,553
  • 11
  • 16
  • I assume you're using some spring-data flavour to get the `@Repository` going? What line is actually throwing the NPE? Can you show the relevant spring context? – Taylor May 02 '16 at 18:40
  • We need to see `AuthenticationSuccessHandlerImpl`. – chrylis -cautiouslyoptimistic- May 02 '16 at 19:00
  • @Taylor List userList = this.dbUtil.executeHQLQuery(userCheckQry, parameters, false); this line throws the error – Swaprks May 03 '16 at 02:41
  • @chrylis i have added the class – Swaprks May 03 '16 at 02:43
  • based on that, `dbUtil` appears to be `null`. You should confirm this via a debugger. Assuming this is the case, it means you've missed something in the spring wiring. Make sure spring is creating an instance of DBUtil for you as you expect (either via package scanning with `@Component` or via explicit instantiation in an xml/java context). – Taylor May 04 '16 at 14:32

1 Answers1

0

I believe in your DBUtil class you need to annotate your SessionFactory so that Spring knows how to wire it.

@Autowired
private SessionFactory sessionFactory;

As it stands now, there is no default constructor and nowhere (that we can see) is the setter being called to set the sessionFactory.

Riggy
  • 1,347
  • 1
  • 14
  • 26