@SpringBootConfiguration
- 用于指示当前类是一个配置类,当你使用@SpringBootApplication时,你其实已经在使用@SpringBootConfiguration了
@EnableAutoConfiguration
- 这个注解是Spring Boot的核心特性之一。它告诉Spring Boot根据添加的jar依赖项、类路径内容和其他因素来自动配置你的Spring应用。
- 例如,如果你在类路径上有spring-boot-starter-web,那么Spring Boot会自动配置Tomcat和Spring MVC。
- 这个注解通常与@SpringBootApplication一起使用,但也可以单独使用。
- 你可以通过spring.autoconfigure.exclude属性来排除某些自动配置。
@ComponentScan
- 这个注解告诉Spring在哪里查找@Component、@Service、@Repository、@Controller等注解的类,以便将它们注册为Spring Bean。
- 如果不指定任何参数,@ComponentScan会扫描当前包及其子包。
- 当你使用@SpringBootApplication时,默认会启用@ComponentScan,并扫描主应用类所在的包及其子包。
- 你可以通过basePackages或basePackageClasses属性来指定要扫描的包。
这个注解是上述三个注解的集合(加上@SpringBootApplication特有的其他功能),通常用于主应用类。当你使用@SpringBootApplication时,你实际上已经在使用@SpringBootConfiguration、@EnableAutoConfiguration和@ComponentScan了。因此,在大多数情况下,你只需在主应用类上添加@SpringBootApplication即可。
代码
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
excludeFilters = {@Filter(
type = FilterType.CUSTOM,
classes = {TypeExcludeFilter.class}
), @Filter(
type = FilterType.CUSTOM,
classes = {AutoConfigurationExcludeFilter.class}
)}
)