コード:CommandLineRunnerと豆(春)私の質問が何であるか
@SpringBootApplication
public class Application {
private static final Logger log = LoggerFactory.getLogger(Application.class);
public static void main(String args[]) {
SpringApplication.run(Application.class);
}
@Bean
public Object test(RestTemplate restTemplate) {
Quote quote = restTemplate.getForObject(
"http://gturnquist-quoters.cfapps.io/api/random", Quote.class);
log.info(quote.toString());
return new Random();
}
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
}
@Bean
public CommandLineRunner run(RestTemplate restTemplate) throws Exception {
return args -> {
Quote quote = restTemplate.getForObject(
"http://gturnquist-quoters.cfapps.io/api/random", Quote.class);
log.info(quote.toString());
};
}
}
私は春に非常に新しいです。 @Beanアノテーションは、オブジェクトがIoCコンテナに保存されていることを理解している限り、正しいですか?
もしそうなら、@Beanを集めた最初のメソッドはすべてそして次にが実行されますか?
私の例では、test()というメソッドを追加しました。これはrun()と同じですが、代わりにObject(Random())を返します。 結果は同じですので、CommandLineRunnerとObjectを使用しています。
CommandLineRunnerを返す理由、つまりrun()のような構文を使用する理由はありますか?
さらに、この時点では、メソッドをコンテナに移動する利点は今までわかりませんでした。なぜそれを実行しないのですか?
ありがとうございました!