Difference between @Bean and @Autowired

前端 未结 6 1808
小蘑菇
小蘑菇 2020-11-28 01:26

Why can\'t I use @Autowired in this case?

@SpringBootApplication
public class Application {

    @Autowired
    BookingService bookingService;

         


        
6条回答
  •  悲哀的现实
    2020-11-28 01:58

    Here's good article about @Autowired annotation: http://www.baeldung.com/spring-autowire

    The @Autowired annotation can instantiate your injectables by defining @ComponentScan("namespace.with.your.components.for.inject") on config class

    @Configuration
    @ComponentScan("com.baeldung.autowire.sample")
    public class AppConfig {}
    

    All components must be marked by @Component annotation. It replaces the @Bean annotation.

提交回复
热议问题