@SpringBootApplication下的三个注解作用@SpringBootConfiguration @EnableAutoConfiguration @ComponentScan

@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}
)}
)

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值