Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
spring理解中的Autowire_Spring_Spring Boot - Fatal编程技术网

spring理解中的Autowire

spring理解中的Autowire,spring,spring-boot,Spring,Spring Boot,当我们在spring中使用@Autowire注释来创建对象时,是否必须在配置类中指定bean?那么@Autowired在springboot中是如何工作的呢?在很多情况下,它不是强制性的,例如在注入声明为@Entity、@Repository、@Controller、@Service或@Component(所有其他的超类)的对象时 在放置@SpringBootApplication的主包中 如果在其他包中声明组件扫描,则始终可以在@SpringBootApplication类中使用@Compon

当我们在spring中使用@Autowire注释来创建对象时,是否必须在配置类中指定bean?那么@Autowired在springboot中是如何工作的呢?

在很多情况下,它不是强制性的,例如在注入声明为@Entity、@Repository、@Controller、@Service或@Component(所有其他的超类)的对象时
在放置@SpringBootApplication的主包中


如果在其他包中声明组件扫描,则始终可以在@SpringBootApplication类中使用@ComponentScan({“package_1”,…“package_n”}),将组件放置在主包之外。请记住将主包包括在列表中。

您正在自动连接的对象必须是Springbean,这意味着它必须使用
@bean
方法在配置类中定义,或者在SpringXML配置中定义,或者以其他方式注册为Springbean<代码>@Autowired在Spring Boot中的工作原理与普通Spring中的工作原理完全相同。