Spring Autowired service is null

11,927

Even though you have annotated the DatabaseDataSource as @Component, you are not injecting, and thus using it as Spring Bean along the way. You simply create it by hand:

public GetAllNotesPresenterImpl(){

    dataSource = new DatabaseDataSource();
    repository = new NotesRepositoryImpl(dataSource);
}

In order to take advantage of injection of this bean:

@Autowired
private NotesService notesService;

you would need to use Spring injection starting from the top (this is only one of the ways you can go about it):

1) Inject the dataSource in the controller:

@RestController
@RequestMapping(value="notes/api")
public class NotesRestController {

private GetAllNotesPresenter getAllNotesPresenter;

@Autowired
private NotesDataSource dataSource;

@RequestMapping(value="/all")
public List<Note> getAll(){
    getAllNotesPresenter = new GetAllNotesPresenterImpl(dataSource);
    return getAllNotesPresenter.getAll();
}

2) Change the constructor of the GetAllNotesPresenterImpl:

public GetAllNotesPresenterImpl(NotesDataSource dataSource){

    this.dataSource = dataSource;
    repository = new NotesRepositoryImpl(dataSource);
}

Other option would be to make GetAllNotesPresenterImpl and NotesRepositoryImpl as spring beans and inject dataSource in the NotesRepositoryImpl directly. The final call is up to you.

Share:
11,927
Zookey
Author by

Zookey

Freelance Android developer. http://zoranpavlovic.net

Updated on June 25, 2022

Comments

  • Zookey
    Zookey almost 2 years

    I have RestController class that calls Presenter to get some data.

    @RestController
    @RequestMapping(value="notes/api")
    public class NotesRestController {
    
    private GetAllNotesPresenter getAllNotesPresenter;
    
    @RequestMapping(value="/all")
    public List<Note> getAll(){
        getAllNotesPresenter = new GetAllNotesPresenterImpl();
        return getAllNotesPresenter.getAll();
    }
    

    }

    Inside Presenter class I call DataSource (not Spring Repository, just DAO class).

    public class GetAllNotesPresenterImpl implements GetAllNotesPresenter {
    
    private NotesDataSource dataSource;
    private NotesRepository repository;
    
    public GetAllNotesPresenterImpl(){
    
        dataSource = new DatabaseDataSource();
        repository = new NotesRepositoryImpl(dataSource);
    }
    @Override
    public List<Note> getAll() {
        return repository.getAll();
    }
    

    }

    This is my Repository class, it is not Spring Repository, it just DAO class.

    public class NotesRepositoryImpl implements NotesRepository {
    
    private NotesDataSource dataSource;
    
    public NotesRepositoryImpl(NotesDataSource dataSource){
        this.dataSource = dataSource;
    }
    
    @Override
    public List<Note> getAll() {
        return dataSource.getAll();
    }
    

    }

    This is my Service class:

    @Service
    @Transactional
    public class NotesServiceImpl implements NotesService {
    
    @Autowired
    private NotesJpaRepository repository;
    
    @Override
    public List<NoteJpa> getAll() {
        return repository.findAll();
    }
    

    }

    Inside DataSource class I want to do @Autowire of Spring service, but I get null pointer exception. Service is always null.

    @Component
    public class DatabaseDataSource implements NotesDataSource {
    
    @Autowired
    private NotesService notesService;
    
    public DatabaseDataSource(){
    }
    
    @Override
    public List<Note> getAll() {
        return notesService.getAll();
    }
    

    }