2017-04-21 9 views
2

私はいくつかのデータを取得するために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(); 
} 

}

+0

上記のコードが正しい場合、 'NotesService'はあなたが言及していないかBeanではない' NotesJpaRepository'を期待しているので、 'NotesService'は決して構築されないでしょう。これが 'NotesRepository'の実装であれば、インスタンス化してBeanとして公開する必要があります。 – meistermeier

+0

"NotesService"インターフェースに@Serviceアノテーションがありますか? – VelNaga

答えて

1

。あなたは、単に手でそれを作成します。

このBeanの注入を利用するためには
public GetAllNotesPresenterImpl(){ 

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

@Autowired 
private NotesService notesService; 

あなたはこれが唯一の1である(上から始まる春の注入を使用する必要があります方法は、あなたは)それについて移動することができます

@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(); 
} 

1)は、コントローラにデータソースを注入します10

2)GetAllNotesPresenterImplのコンストラクタを変更し

public GetAllNotesPresenterImpl(NotesDataSource dataSource){ 

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

その他のオプションは、春の豆としてGetAllNotesPresenterImplNotesRepositoryImplを作り、直接NotesRepositoryImplでDataSourceを注入するだろう。最後の呼び出しはあなた次第です。

+0

うん、これは動作します:)私はいくつかの説明が必要です。 NotesDataSourceの実装が複数ある場合はどうなりますか。現在のところ、私は、NotesDataSoruceを実装しているRestartControllerクラスの内部に挿入するDatabaseDataSourceしか持っていません。 – Zookey

+0

@Qualifier注釈を使用して、注入に明示的に使用する実装を指定します。 –

+0

ありがとう!あなたが望むなら、それを追加することができます。例えば、多くの人がこの種の質問を検索するためです。おそらく、誰かにとって役に立ちます。 – Zookey

関連する問題