SpringBoot這些常用注解你該知道
@SpringBootApplication
這是 Spring Boot 最最最核心的注解,用在 Spring Boot 主類上,標(biāo)識(shí)這是一個(gè) Spring Boot 應(yīng)用,用來(lái)開啟 Spring Boot 的各項(xiàng)能力。
- @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
開啟自動(dòng)配置注解,SpringBoot 就能根據(jù)當(dāng)前類路徑下的包或者類來(lái)配置 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 添加的一個(gè)注解,用來(lái)代替 applicationContext.xml 配置文件,通過(guò)注解來(lái)配置Bean。
- @Configuration
- public class WebConfig implements WebMvcConfigurer {
- }
@ComponentScan
這是 Spring 3.1 添加的一個(gè)注解,用來(lái)代替配置文件中的 component-scan 配置,開啟組件掃描,即自動(dòng)掃描包路徑下的 @Component 注解進(jìn)行注冊(cè) bean 實(shí)例到 context 中。
- @ComponentScan(basePackages = {"com.pack.a", "com.jack.b"})
- public class SqlSessionFactoryConfig {
- }
@Conditional
這是 Spring 4.0 添加的新注解,用來(lái)標(biāo)識(shí)一個(gè) Spring Bean 或者 Configuration 配置文件,當(dāng)滿足指定的條件才開啟配置。
- @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
當(dāng)容器中有指定的 Bean 才開啟配置。
@ConditionalOnMissingBean
當(dāng)容器中沒有指定的 Bean 才開啟配置。
- @ConditionalOnMissingBean({ DataSource.class, XADataSource.class })
- protected static class EmbeddedConfiguration {
- }
@ConditionalOnClass
組合 @Conditional 注解,當(dāng)容器中有指定的 Class 才開啟配置。
- @ConditionalOnClass({ RabbitTemplate.class, Channel.class })
- public class RabbitAutoConfiguration {
- }
@ConditionalOnMissingClass
當(dāng)容器中沒有指定的 Class 才開啟配置。
@ConditionalOnWebApplication
當(dāng)前項(xiàng)目類型是 WEB 項(xiàng)目才開啟配置。
當(dāng)前項(xiàng)目有以下 3 種類型。
- /**
- * Any web application will match.
- */
- ANY,
- /**
- * Only servlet-based web application will match.
- */
- SERVLET,
- /**
- * Only reactive-based web application will match.
- */
- REACTIVE
@ConditionalOnNotWebApplication
當(dāng)前項(xiàng)目類型不是 WEB 項(xiàng)目才開啟配置。
@ConditionalOnProperty
當(dāng)指定的屬性有指定的值時(shí)才開啟配置。
- @Bean
- @ConditionalOnProperty(prefix = "spring.rabbitmq", name = "dynamic", matchIfMissing = true)
- public AmqpAdmin amqpAdmin(ConnectionFactory connectionFactory) {
- return new RabbitAdmin(connectionFactory);
- }
@ConditionalOnExpression
當(dāng) SpEL 表達(dá)式為 true 時(shí)才開啟配置。
@ConditionalOnJava
當(dāng)運(yùn)行的 Java JVM 在指定的版本范圍時(shí)才開啟配置。
@ConditionalOnResource
當(dāng)類路徑下有指定的資源才開啟配置。
@ConditionalOnJndi
當(dāng)指定的 JNDI 存在時(shí)才開啟配置。
@ConditionalOnSingleCandidate
當(dāng)指定的 class 在容器中只有一個(gè) Bean,或者同時(shí)有多個(gè)但為首選時(shí)才開啟配置。
@ConfigurationProperties
用來(lái)加載額外的配置(如 .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 注解使用,用來(lái)開啟對(duì) @ConfigurationProperties 注解配置 Bean 的支持。
- @Configuration
- @ConditionalOnClass({ DataSource.class, EmbeddedDatabaseType.class })
- @EnableConfigurationProperties(DataSourceProperties.class)
- public class DataSourceAutoConfiguration {
- }
@AutoConfigureAfter
用在自動(dòng)配置類上面,表示該自動(dòng)配置類需要在另外指定的自動(dòng)配置類配置完之后。
如 Mybatis 的自動(dòng)配置類,需要在數(shù)據(jù)源自動(dòng)配置類之后。
- @AutoConfigureAfter({ DataSourceAutoConfiguration.class, MybatisLanguageDriverAutoConfiguration.class })
- public class MybatisAutoConfiguration implements InitializingBean {
- }
@AutoConfigureBefore
表示該自動(dòng)配置類需要在另外指定的自動(dòng)配置類配置之前。
@Import
這是 Spring 3.0 添加的新注解,用來(lái)導(dǎo)入一個(gè)或者多個(gè) @Configuration 注解修飾的類,這在 SpringBoot 里面應(yīng)用很多。
- @Import(CachingConfigurationSelector.class)
- public @interface EnableCaching {
- }
@ImportResource
這是 Spring 3.0 添加的新注解,用來(lái)導(dǎo)入一個(gè)或者多個(gè) Spring 配置文件,這對(duì) Spring Boot 兼容老項(xiàng)目非常有用,因?yàn)橛行┡渲脽o(wú)法通過(guò) Java Config 的形式來(lái)配置就只能用這個(gè)注解來(lái)導(dǎo)入。
- @ImportResource({ "classpath:spring/application-*.xml" })
- @SpringBootApplication
- public class AppApplication {
- }
@RestController
該注解是@ResponseBody + @Controller的組合。返回的內(nèi)容是return 的內(nèi)容,無(wú)法返回jsp或html頁(yè)面等視圖文件。
- @RestController
- @RequestMapping("/users")
- public class UsersController {
- }
@RequestMapping
映射請(qǐng)求路徑。
@GetMapping
映射Get請(qǐng)求
@PostMapping
映射post請(qǐng)求
@PatchMapping
映射method為patch的請(qǐng)求。一般用于個(gè)別屬性的修改操作
@PutMapping
創(chuàng)建新的資源或替換請(qǐng)求負(fù)載目標(biāo)資源的表示。Put冪等,POST不是
@DeleteMapping
刪除資源
@RequestBody
指示接口參數(shù)接受的是該請(qǐng)求的主體內(nèi)容。
@PathVariable
接受請(qǐng)求路徑中的占位符的值。