了解这些注解可以帮助你更深入的理解spring boot的原理
JDK-5之后Java引入了注解的概念,它与类和接口属于同一类型。注解理论上是一系列元数据,提供数据来解释程序代码,但注解不是被解释代码本身的一部分。
注解对代码的运行效果没有直接影响。注解与注释非常相似,除了注解是为开发人员提高代码的可读性和可维护性,注解为编译器和虚拟机提供信息。
虚拟机可以在程序运行的过程中获取注解信息,我们可以利用这些信息做很多事情。 spring boot 的 Convention over configuration 特性依赖于注解。今天就来说说spring boot中你必须知道的一些注解。
@Configuration
@Configuration 是一个类级别的注解。需要注意的是,这个注解是spring框架的注解,不是spring-boot注解。不过,在 Spring 框架的早期,XML 配置文件更受欢迎。
因此,没有人知道@configuration。 @configuration 注解用于将一个类声明为配置类。它通常与@bean 结合使用。值得一提的是,默认配置类的初始化优先级高于普通组件。
@Configuration
public class MyAutoConfiguration {
@Bean
public User user() {
return new User();
}
}@Bean
@Bean 是一个方法级别的注解。 spring boot 启动时会调用使用@bean 声明的方法(仅在配置类中),并将返回值注册为spring 容器中的特殊Java Bean @Bean 可以与@lazy 和@order 注解一起使用。 如果不使用@order,则默认初始化顺序为代码顺序。
@Configuration
public class MyAutoConfiguration {
@Bean
public User1 user1() {
return new User1();
}
@Bean
public User2 user2() {
return new User2();
}
}@ConfigurationProperties
在spring boot中,当需要获取配置数据时,除了spring提供的@value注解,spring boot还提供了@ConfigurationProperties注解。
相比@value,@ConfigurationProperties 更方便,可以批量获取配置。
只要在属性类中加入这个注解并指定前缀,相应的配置文件数据就会自动填充到bean中。
simulator:
executor:
coreThreadNumber: 4
maxThreadNumber: 50@Configuration
@ConfigurationProperties(prefix = "simulator.executor")
public class SimulatorProperties {
private Integer coreThreadNumber;
private Integer maxThreadNumber;
public Integer getCoreThreadNumber() {
return coreThreadNumber;
}
public void setCoreThreadNumber(Integer coreThreadNumber) {
this.coreThreadNumber = coreThreadNumber;
}
public Integer getMaxThreadNumber() {
return maxThreadNumber;
}
public void setMaxThreadNumber(Integer maxThreadNumber) {
this.maxThreadNumber = maxThreadNumber;
}
}@ComponentScan
@ComponentScan 注解可以根据用户配置的路径扫描相应的组件(如@component、@service、@controller)并注册到spring容器中。
如果使用@springbootApplication注解启动服务,并且启动类放在根包中,则不需要添加任何参数,因为@springbootApplication已经包含@componentScan注解,默认扫描的根路径为 当前类所在的路径。
但是,如果第三方库的某些组件要在spring容器中注册,则需要使用@componentScan。
@ComponentScan(basePackages={"com.test.simulator","com.test2"})
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}@ConditionalOnClass 和 @ConditionalOnMissClass
@ConditionaloOnClass 注解可以在类和方法上声明。 如果使用@ConditionalOnClass 注解如下。
@Configuration
@ConditionalOnClass(KafkaTemplate.class)
public class DefaultKafkaAutoConfiguration {
@Bean
public KafkaTemplate KafkaTemplate() {
return new KafkaTemplate();
}
}那么KafkaTemplate.class只能在classpath中找到,这个配置类会被正确扫描并注册到spring容器中。 @ConditionalOnMissClass 正好相反。 只有在类路径中找不到定义的类时,才会构建并注册配置类。
@ConditionalOnBean 和 @ConditionalOnMissingBean
与上面条件系列的注解类似,@ConditionalOnBean 和@ConditionalOnMissingBean 表示只有在特定的bean存在或不存在时才会构建和注册类。
@ConditionalOnProperty
@ConditionalOnProperty 也是条件系列的注解。 这个注解可以根据用户的配置来确定构建bean的条件。
@Component
@ConditionalOnProperty(value = "audit.persist.sink",havingValue = "sftp",matchIfMissing = true)
public class SftpPersister extends PersistHelper {
}这个例子表明,只有当 key audit.persist.sinkissftpor 的值没有定义时,spring 才会识别和构建类 SftpPersister。
结论
Spring有很多注解来简化我们的开发。 本文简单介绍一些我认为有必要了解的注解。 作为一个java开发工程师,你应该了解这些注解。 最后,感谢您的阅读。 希望这篇文章能让你更多地了解spring boot。
| 留言与评论(共有 0 条评论) “” |