SpringBoot這些常用注解你該知道
@SpringBootApplication
這是 Spring Boot 最最最核心的注解,用在 Spring Boot 主類上,標識這是一個 Spring Boot 應用,用來開啟 Spring Boot 的各項能力。
- @SpringBootApplication
- public class BaseWebApplication extends SpringBootServletInitializer {
- @Override
- protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
- return builder.sources(BaseWebApplication.class);
- }
- public static void main(String[] args) {
- SpringApplication.run(BaseWebApplication.class, args);
- }
- }
@EnableAutoConfiguration
開啟自動配置注解,SpringBoot 就能根據當前類路徑下的包或者類來配置 Bean。
- @Target(ElementType.TYPE)
- @Retention(RetentionPolicy.RUNTIME)
- @Documented
- @Inherited
- @SpringBootConfiguration
- @EnableAutoConfiguration
- @ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
- @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
- public @interface SpringBootApplication {
- }
@Configuration
這是 Spring 3.0 添加的一個注解,用來代替 applicationContext.xml 配置文件,通過注解來配置Bean。
- @Configuration
- public class WebConfig implements WebMvcConfigurer {
- }
@ComponentScan
這是 Spring 3.1 添加的一個注解,用來代替配置文件中的 component-scan 配置,開啟組件掃描,即自動掃描包路徑下的 @Component 注解進行注冊 bean 實例到 context 中。
- @ComponentScan(basePackages = {"com.pack.a", "com.jack.b"})
- public class SqlSessionFactoryConfig {
- }
@Conditional
這是 Spring 4.0 添加的新注解,用來標識一個 Spring Bean 或者 Configuration 配置文件,當滿足指定的條件才開啟配置。
- @Bean
- @Conditional({SEEConditional.class})
- public ServerEndpointExporter serverEndpointExporter (){
- return new ServerEndpointExporter();
- }
- public class SEEConditional implements Condition {
- @Override
- public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
- String active = context.getEnvironment().getProperty("app.profiles.active") ;
- return !"prod".equals(active) ;
- }
- }
@ConditionalOnBean
當容器中有指定的 Bean 才開啟配置。
@ConditionalOnMissingBean
當容器中沒有指定的 Bean 才開啟配置。
- @ConditionalOnMissingBean({ DataSource.class, XADataSource.class })
- protected static class EmbeddedConfiguration {
- }
@ConditionalOnClass
組合 @Conditional 注解,當容器中有指定的 Class 才開啟配置。
- @ConditionalOnClass({ RabbitTemplate.class, Channel.class })
- public class RabbitAutoConfiguration {
- }
@ConditionalOnMissingClass
當容器中沒有指定的 Class 才開啟配置。
@ConditionalOnWebApplication
當前項目類型是 WEB 項目才開啟配置。
當前項目有以下 3 種類型。
- /**
- * Any web application will match.
- */
- ANY,
- /**
- * Only servlet-based web application will match.
- */
- SERVLET,
- /**
- * Only reactive-based web application will match.
- */
- REACTIVE
@ConditionalOnNotWebApplication
當前項目類型不是 WEB 項目才開啟配置。
@ConditionalOnProperty
當指定的屬性有指定的值時才開啟配置。
- @Bean
- @ConditionalOnProperty(prefix = "spring.rabbitmq", name = "dynamic", matchIfMissing = true)
- public AmqpAdmin amqpAdmin(ConnectionFactory connectionFactory) {
- return new RabbitAdmin(connectionFactory);
- }
@ConditionalOnExpression
當 SpEL 表達式為 true 時才開啟配置。
@ConditionalOnJava
當運行的 Java JVM 在指定的版本范圍時才開啟配置。
@ConditionalOnResource
當類路徑下有指定的資源才開啟配置。
@ConditionalOnJndi
當指定的 JNDI 存在時才開啟配置。
@ConditionalOnSingleCandidate
當指定的 class 在容器中只有一個 Bean,或者同時有多個但為首選時才開啟配置。
@ConfigurationProperties
用來加載額外的配置(如 .properties 文件),可用在 @Configuration 注解類,或者 @Bean 注解方法上面。
- @Bean
- @ConfigurationProperties(prefix = DataSourceProperties.PREFIX)
- public DataSource dataSource() {
- DataSourceBuilder factory = DataSourceBuilder
- .create(this.properties.getClassLoader())
- .driverClassName(this.properties.getDriverClassName())
- .url(this.properties.getUrl()).username(this.properties.getUsername())
- .password(this.properties.getPassword());
- if (this.properties.getType() != null) {
- factory.type(this.properties.getType());
- }
- return factory.build();
- }
@EnableConfigurationProperties
配合 @ConfigurationProperties 注解使用,用來開啟對 @ConfigurationProperties 注解配置 Bean 的支持。
- @Configuration
- @ConditionalOnClass({ DataSource.class, EmbeddedDatabaseType.class })
- @EnableConfigurationProperties(DataSourceProperties.class)
- public class DataSourceAutoConfiguration {
- }
@AutoConfigureAfter
用在自動配置類上面,表示該自動配置類需要在另外指定的自動配置類配置完之后。
如 Mybatis 的自動配置類,需要在數據源自動配置類之后。
- @AutoConfigureAfter({ DataSourceAutoConfiguration.class, MybatisLanguageDriverAutoConfiguration.class })
- public class MybatisAutoConfiguration implements InitializingBean {
- }
@AutoConfigureBefore
表示該自動配置類需要在另外指定的自動配置類配置之前。
@Import
這是 Spring 3.0 添加的新注解,用來導入一個或者多個 @Configuration 注解修飾的類,這在 SpringBoot 里面應用很多。
- @Import(CachingConfigurationSelector.class)
- public @interface EnableCaching {
- }
@ImportResource
這是 Spring 3.0 添加的新注解,用來導入一個或者多個 Spring 配置文件,這對 Spring Boot 兼容老項目非常有用,因為有些配置無法通過 Java Config 的形式來配置就只能用這個注解來導入。
- @ImportResource({ "classpath:spring/application-*.xml" })
- @SpringBootApplication
- public class AppApplication {
- }
@RestController
該注解是@ResponseBody + @Controller的組合。返回的內容是return 的內容,無法返回jsp或html頁面等視圖文件。
- @RestController
- @RequestMapping("/users")
- public class UsersController {
- }
@RequestMapping
映射請求路徑。
@GetMapping
映射Get請求
@PostMapping
映射post請求
@PatchMapping
映射method為patch的請求。一般用于個別屬性的修改操作
@PutMapping
創建新的資源或替換請求負載目標資源的表示。Put冪等,POST不是
@DeleteMapping
刪除資源
@RequestBody
指示接口參數接受的是該請求的主體內容。
@PathVariable
接受請求路徑中的占位符的值。