spring理解中的Autowire
当我们在spring中使用@Autowire注释来创建对象时,是否必须在配置类中指定bean?那么@Autowired在springboot中是如何工作的呢?在很多情况下,它不是强制性的,例如在注入声明为@Entity、@Repository、@Controller、@Service或@Component(所有其他的超类)的对象时spring理解中的Autowire,spring,spring-boot,Spring,Spring Boot,当我们在spring中使用@Autowire注释来创建对象时,是否必须在配置类中指定bean?那么@Autowired在springboot中是如何工作的呢?在很多情况下,它不是强制性的,例如在注入声明为@Entity、@Repository、@Controller、@Service或@Component(所有其他的超类)的对象时 在放置@SpringBootApplication的主包中 如果在其他包中声明组件扫描,则始终可以在@SpringBootApplication类中使用@Compon
在放置@SpringBootApplication的主包中
如果在其他包中声明组件扫描,则始终可以在@SpringBootApplication类中使用@ComponentScan({“package_1”,…“package_n”}),将组件放置在主包之外。请记住将主包包括在列表中。您正在自动连接的对象必须是Springbean,这意味着它必须使用
@bean
方法在配置类中定义,或者在SpringXML配置中定义,或者以其他方式注册为Springbean<代码>@Autowired在Spring Boot中的工作原理与普通Spring中的工作原理完全相同。