Issue with DAO java file being called

I am new to Java spring and currently within the code I am updating there is a UserDetailsDao class I am trying to call in another java file.

@Repository public class UserDetailsDao {
@Autowired
NamedParameterJdbcTemplate template; //blah blah functions}

This is the java file I am trying to call a function I need from the DAO class file to get:

public class LoginAuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler{

public final Integer SESSION_TIMEOUT_IN_SECONDS = 7500;
private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();

@Override
public void onAuthenticationSuccess(HttpServletRequest request,
                                    HttpServletResponse response,
                                    Authentication authentication)
                                    throws ServletException, IOException {
    
    HttpSession session = request.getSession();
    session.setMaxInactiveInterval(SESSION_TIMEOUT_IN_SECONDS);
    
    String redirectPath = "/";
    
    User u = new User();
    
    //UserDetailsDao userDetailsDao = new UserDetailsDao();
   
   // u = userDetailsDao.getUserDetailedInfo(request.getParameter("username"));
   //gimme my data!
    UserDetailsServiceImpl utest = userDetailsService();
    u = utest.getUserDetails(request.getParameter("username"));
    request.setAttribute("user", u);
    
    redirectStrategy.sendRedirect(request, response, redirectPath);
}

}

I tired to call a bean of the service imp file that has this setup working and call functions via here:

    @Bean
public UserDetailsServiceImpl userDetailsService() {
    return new UserDetailsServiceImpl();
}

or

@Autowired
private UserDetailsDao userDetailsDao;

When i debug the logging in portion of the application once I get to the LoginAuthenticationSuccessHandler class file that has call to a function within here to get user detailed data of a user the UserDetailsDao is always null regardless of the various ways that I try to call the DAO class file. This is where I am so confused, in comparison to another file within the code, UserDetailsServiceImpl , that is able to use UserDetailsDao simply by having the at autowired and using that var to call functions within the DAO with no null to the DAO iteslef:

@Autowired
private UserDetailsDao userDetailsDao;

in its class file. I have tired reading up the various annotations, looking up similar errors, reading others examples and it doesn’t seem to stick to my mind. My guess is it has something to do with the annotations as well as the class file LoginAuthenticationSuccessHandler in which I am trying to call the DAO. If someone could point to me what I am doing wrong and have a link of something where I can learn more.

Answer

This happens because your LoginAuthenticationSuccessHandler is not a Bean so Spring can’t inject another Bean into it. In this scenario, you should provide your UserDetailsDao bean to the LoginAuthenticationSuccessHandler class, like so:

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsDao userDetailsDao;

    public AuthenticationSuccessHandler successHandler(){
        return new LoginAuthenticationSuccessHandler(this.userDetailsDao);
    }
}

And

public class LoginAuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {

    private final UserDetailsDao userDetailsDao;

    public LoginAuthenticationSuccessHandler(UserDetailsDao userDetailsDao) {
        this.userDetailsDao = userDetailsDao;
    }
}