Search code examples
javaspringspring-bootspring-datarepository

Spring Repository is null despite being autowired


I'm trying to create a user login system for an application. This login system works by allowing a user to register a profile and save it to a database. A login interface is then created in the logininterface class where the user inputs their username and password. This class should then call user service which access' a repository to locate the username and password in the database. The issue persists when trying to retrieve a user's details from the database, the repository used returns null. I think this is due to it not being initialised properly, however I cannot find the correct way to initialise the repository. Code snippets are included below:

Main class

@SpringBootApplication
public class Application implements ApplicationRunner {

    @Autowired
    UserRepo userRepo;

    public static void main(String[] args) {

        SpringApplication.run(Application.class, args);
    }


    @Override
    public void run(ApplicationArguments args) throws Exception {


        System.setProperty("java.awt.headless", "false");

      
        LoginInterface.createLogin();
    }
}

User Repository

@Repository
public interface UserRepo extends JpaRepository<User, Long> {
    Optional<User> findByuserName(String userName);
}

User Service

@Service
public class UserService implements UserDetailsService {


    private final static String not_found = "user with username %s not found";

    @Autowired
    public UserRepo userRepo;

    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        return userRepo.findByuserName(username).orElseThrow(() -> new UsernameNotFoundException(String.format(not_found, username)));
    }
}

Login Interface

public class LoginInterface implements ActionListener {


    private UserService userService = new UserService();

    public static void createLogin(){
        button.addActionListener((ActionListener) new LoginInterface());

      

    }

    @Override
    public void actionPerformed(ActionEvent e) {


        String user = username.getText();
        String password = username.getText();

        User theUser = (User) userService.loadUserByUsername(user);
        String theUsername = theUser.getUsername();
        String thePassword = theUser.getPassword();

        if(user.equals(theUsername) && password.equals(thePassword)){
            JOptionPane.showMessageDialog(null, "Login Successful");
        }
        else{
            JOptionPane.showMessageDialog(null, "Username or Password mismatch");
        }
    }
}

Solution

  • The problem in your code is here:

    private UserService userService = new UserService();
    

    You expect that the object will be created by the container through dependency injection, but instead, you create it manually. It does not work like that.

    Try something like this:

    @Service
    public class LoginInterface implements ActionListener {
       //
       @Autowired
       private UserService userService
       //
    }