春にautowingly
コンストラクタ
@Autowired
public AppServiceImpl(AppRepository appRepository) {
this.appRepository = appRepository;
}
メソッド注入
@Autowired
public setRepository(AppRepository appRepository) {
this.appRepository = appRepository;
}
フィールド注入
推奨されません.ユニットテストをハード.
@Autowired
private AppRepository appRepository;
オプションの依存関係
依存性が存在する場合のみ注入します.
@Autowired(required=false)
AppService appService;
public void method() {
if(appService != null) {
...
}
}
オプションの使用 @Autowired
Optional<AppService> appService;
public void method() {
appService.ifPresent(s -> {
...
});
}
修飾子注釈
コンポーネント名が指定されない場合、それらは自動生成されます.指定された場合、1つ以上のBeanクラスが同じインターフェイスを実装している場合は、あいまいさを許容します.
@Component
public class AppServiceImpl implements AppService {
@Autowired
public AppServiceImpl(@Qualifier("jdbcRepository") AppRepository appRepository) {
this.appRepository = appRepository;
}
}
@Component("jdbcRepository")
public class JdbcRepositoryImpl implements AppRepository {
...
}
Reference
この問題について(春にautowingly), 我々は、より多くの情報をここで見つけました https://dev.to/eidher/autowiring-in-spring-f89テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol