6
@Bean和@Autowired之间的区别
为什么@Autowired在这种情况下不能使用? @SpringBootApplication public class Application { @Autowired BookingService bookingService; public static void main(String[] args) { bookingService.book("Alice", "Bob", "Carol"); } } 但可以使用 @Bean @SpringBootApplication public class Application { @Bean BookingService bookingService() { return new BookingService(); } public static void main(String[] args) { ApplicationContext ctx = SpringApplication.run(Application.class, args); BookingService bookingService = ctx.getBean(BookingService.class); …