常用注解

@SpringBootApplication
包含了@ComponentScan@Configuration@EnableAutoConfiguration

@ComponentScan
让Spring Boot扫描到@Configuration类并加入到上下文

@Configuration
相当于Spring的XML文件

@Bean
相当于XML里配置的bean

@EnableAutoConfiguration
帮助SpringBoot应用将所有符合条件的@Configuration配置都加载到当前SpringBoot创建并使用的IoC容器

@Import
导入其他配置类

@Value
注入properties配置的属性值

@ConfigurationProperties
配合@ComponentScan使用,属性注入到Bean对象。@Value一个个注入有点笨重

@Qualifier
注入同个类型的bean,用此注解指定,与@Autowired配合使用

@RestController、@PostMapping

@Conditinal
根据boolean是否加载注入

@Autowired、@Resource
两者功能都是注入。
@Autowired是Spring提供的(org.springframework.beans.factory.annotation)注解,按照类型(byType)装载的,默认情况不允许为null,否则需要设置required=false。如果需要按照名称(byName)来装配,需要结合@Qualifier来使用;
@Resource是J2EE提供的(javax.annotation.Resource),如果指定name则通过byName装配,如果指定type则通过byType装配,都没指定默认用byName

  1. @Autowired
  2. @Qualifier(required = false, value = demoInfoService”)
  3. private DemoInfoService demoInfoService;
  4. @Resource(name = "demoInfoService", type = "DemoInfoService")
  5. private DemoInfoService demoInfoService;

@ConditionalOnProperty
控制@Configuration是否生效

  1. @Configuration
  2. @ConditionalOnProperty(prefix = "filter", name = "loginFilter", havingValue = "true")
  3. public class FilterConfig {
  4. //prefix为配置文件中的前缀,
  5. //name为配置的名字
  6. //havingValue是与配置的值对比值,当两个值相同返回true,配置类生效.
  7. }


@Order @DependOn
@Order :控制执行顺序,数值越小执行优先级越高
@DependsOn:控制bean初始化顺序

  1. @Order(1)
  2. @Service
  3. public class LexerSqlParse implements SqlParse {
  4. @Override
  5. public String parse(String sql) {
  6. return null;
  7. }
  8. }
  9. @Order(2)
  10. @Service
  11. public class RegexSqlParse implements SqlParse {
  12. @Override
  13. public String parse(String sql) {
  14. return null;
  15. }
  16. }
  17. @Component
  18. public class SqlLogAspect {
  19. // sqlParseList[0] = LexerSqlParse
  20. // sqlParseList[1] = LexerSqlParse
  21. @Autowired
  22. private List<SqlParse> sqlParseList;
  23. }

@ConditionalOnMissingBean
它是修饰bean的一个注解,主要实现的是,当你的bean被注册之后,如果而注册相同类型的bean,就不会成功,它会保证你的bean只有一个,即你的实例只有一个。
如果不加@ConditionalOnMissingBean,当你注册多个相同的bean时,会出现异常,以此来告诉开发人员。
image.png
@ConditionalOnBean

https://blog.csdn.net/wtl1992/article/details/122210599