私はいくつかのデータを取得するためにPresenterを呼び出すRestControllerクラスを持っています。春のAutowiredサービスがnullです
@RestController
@RequestMapping(value="notes/api")
public class NotesRestController {
private GetAllNotesPresenter getAllNotesPresenter;
@RequestMapping(value="/all")
public List<Note> getAll(){
getAllNotesPresenter = new GetAllNotesPresenterImpl();
return getAllNotesPresenter.getAll();
}
}
インサイドプレゼンタークラスIは(リポジトリ、単にDAOクラスを春ではない)データソースを呼び出します。
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();
}
}
これは私のリポジトリクラスであり、それは春のリポジトリ、それだけでDAOクラスではありません。
public class NotesRepositoryImpl implements NotesRepository {
private NotesDataSource dataSource;
public NotesRepositoryImpl(NotesDataSource dataSource){
this.dataSource = dataSource;
}
@Override
public List<Note> getAll() {
return dataSource.getAll();
}
}
は、これは私のサービスクラスです:
@Service
@Transactional
public class NotesServiceImpl implements NotesService {
@Autowired
private NotesJpaRepository repository;
@Override
public List<NoteJpa> getAll() {
return repository.findAll();
}
}
インサイドDataSourceクラスIは、春のサービスの@Autowireをしたいが、私はnullポインタ例外を取得。サービスは常にnullです。あなたはDatabaseDataSource
@Component
として、あなたが注入されていないので、道に沿って春の豆としてそれを使用して注釈を付けているにもかかわらず、
@Component
public class DatabaseDataSource implements NotesDataSource {
@Autowired
private NotesService notesService;
public DatabaseDataSource(){
}
@Override
public List<Note> getAll() {
return notesService.getAll();
}
}
上記のコードが正しい場合、 'NotesService'はあなたが言及していないかBeanではない' NotesJpaRepository'を期待しているので、 'NotesService'は決して構築されないでしょう。これが 'NotesRepository'の実装であれば、インスタンス化してBeanとして公開する必要があります。 – meistermeier
"NotesService"インターフェースに@Serviceアノテーションがありますか? – VelNaga