0

below is my code for service layer but for some reason my @Repository LoginDataAccess is null.

@Service
public class LoginService implements BeanFactoryAware {

    @Autowired
    private LoginDataAccess loginDataAccess;

    public void addUserLoginDetails(LoginData loginData) {
        LoginDetails loginDetails = new LoginDetails();
        loginDetails.setUsername(loginData.getUsername());
        loginDetails.setPassword(loginData.getPassword());
        if(loginDataAccess == null) {
            System.out.println("loginDAtaAccess is null"); // this prints on console
        }

        loginDataAccess.insertLoginDetails(loginDetails); // throws NullPointerException
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("setting beanfactory");
        System.out.println(beanFactory.containsBean("loginDataAccess")); // this prints true 
    }

}
Halvor Holsten Strand
  • 19,829
  • 17
  • 83
  • 99
Meena Chaudhary
  • 9,909
  • 16
  • 60
  • 94
  • What is `LoginDataAccess` - class or interface ?? Have you annotated `LoginDataAccess` ?? If you have not annotated `LoginDataAccess` then Spring will not be able to inject the object at runtime .. – hagrawal7777 Oct 04 '15 at 09:43
  • 2
    Add more details including configs – Ali Dehghani Oct 04 '15 at 09:43

1 Answers1

0

My guess is that you instantiated your LoginService somewhere, using the java "new" keyword. If you did so, then Spring has no way to know about its existence, hence cannot autowire anything. To validate this guess, I would need to see the place where you use this LoginService.

Maybe this question can help you.

Community
  • 1
  • 1
Benjamin Amelot
  • 103
  • 1
  • 8