1、注解
@EnableAutoConfiguration
官方文档:The
@EnableAutoConfiguration
annotation is often placed on your main class, and it implicitly defines a base “search package” for certain items. For example, if you are writing a JPA application, the package of the@EnableAutoConfiguration
annotated class will be used to search for@Entity
items.@EnableAutoConfiguration通常用在主类上,它隐含地定义了某些项目的基本“搜索包”。如果您正在编写JPA应用程序,则@EnableAutoConfiguration注释类的包将用于搜索@Entity项。
官方文档:You should only ever add one@EnableAutoConfiguration
annotation. We generally recommend that you add it to your primary@Configuration
class
您应该只添加一个@EnableAutoConfiguration注释。我们通常建议您将其添加到主要的@Configuration类中。@Configuration
如果需要在启动SpringApplication.run()时,需要加载xml文件,官方建议在main方法的类上加上该注解。也可以在多个类上使用该注解。
@Import注解可以导入配置类,另外使用
@ComponentScan
注解可以自动装载所有spring组件,包括@Configuration
注解的类。-
@ImportResource
加载xml文件 @ComponentScan
自动扫描装载spring组件,比如(@Component
,@Service
,@Repository
,@Controller
etc.)
@SpringBootApplication
使用@SpringBootApplication注释相当于使用@Configuration,@EnableAutoConfiguration和@ComponentScan及其默认属性:
package com.example.myproject; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }