常用注解
@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;
@Autowired@Qualifier(required = false, value = “demoInfoService”)private DemoInfoService demoInfoService;@Resource(name = "demoInfoService", type = "DemoInfoService")private DemoInfoService demoInfoService;
@ConditionalOnProperty
控制@Configuration是否生效
@Configuration@ConditionalOnProperty(prefix = "filter", name = "loginFilter", havingValue = "true")public class FilterConfig {//prefix为配置文件中的前缀,//name为配置的名字//havingValue是与配置的值对比值,当两个值相同返回true,配置类生效.}
@Order @DependOn@Order :控制执行顺序,数值越小执行优先级越高 @DependsOn:控制bean初始化顺序
@Order(1)@Servicepublic class LexerSqlParse implements SqlParse {@Overridepublic String parse(String sql) {return null;}}@Order(2)@Servicepublic class RegexSqlParse implements SqlParse {@Overridepublic String parse(String sql) {return null;}}@Componentpublic class SqlLogAspect {// sqlParseList[0] = LexerSqlParse// sqlParseList[1] = LexerSqlParse@Autowiredprivate List<SqlParse> sqlParseList;}
@ConditionalOnMissingBean
它是修饰bean的一个注解,主要实现的是,当你的bean被注册之后,如果而注册相同类型的bean,就不会成功,它会保证你的bean只有一个,即你的实例只有一个。
如果不加@ConditionalOnMissingBean,当你注册多个相同的bean时,会出现异常,以此来告诉开发人员。
@ConditionalOnBean
