自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

SpringBoot這些常用注解你該知道

開發(fā) 前端
本篇給大家介紹SpringBoot的一些些常用注解,用在 Spring Boot 主類上,標(biāo)識(shí)這是一個(gè) Spring Boot 應(yīng)用,用來(lái)開啟 Spring Boot 的各項(xiàng)能力。希望能夠幫助到你!

[[390142]]

@SpringBootApplication

這是 Spring Boot 最最最核心的注解,用在 Spring Boot 主類上,標(biāo)識(shí)這是一個(gè) Spring Boot 應(yīng)用,用來(lái)開啟 Spring Boot 的各項(xiàng)能力。

  1. @SpringBootApplication 
  2. public class BaseWebApplication extends SpringBootServletInitializer { 
  3.  
  4.     @Override 
  5.     protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { 
  6.         return builder.sources(BaseWebApplication.class); 
  7.     } 
  8.  
  9.     public static void main(String[] args) { 
  10.         SpringApplication.run(BaseWebApplication.class, args); 
  11.     } 

@EnableAutoConfiguration

開啟自動(dòng)配置注解,SpringBoot 就能根據(jù)當(dāng)前類路徑下的包或者類來(lái)配置 Bean。

  1. @Target(ElementType.TYPE) 
  2. @Retention(RetentionPolicy.RUNTIME) 
  3. @Documented 
  4. @Inherited 
  5. @SpringBootConfiguration 
  6. @EnableAutoConfiguration 
  7. @ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class), 
  8.         @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) }) 
  9. public @interface SpringBootApplication { 
  10. }   

 @Configuration

這是 Spring 3.0 添加的一個(gè)注解,用來(lái)代替 applicationContext.xml 配置文件,通過(guò)注解來(lái)配置Bean。

  1. @Configuration 
  2. public class WebConfig implements WebMvcConfigurer { 
  3. }   

 @ComponentScan

這是 Spring 3.1 添加的一個(gè)注解,用來(lái)代替配置文件中的 component-scan 配置,開啟組件掃描,即自動(dòng)掃描包路徑下的 @Component 注解進(jìn)行注冊(cè) bean 實(shí)例到 context 中。

  1. @ComponentScan(basePackages = {"com.pack.a""com.jack.b"}) 
  2. public class SqlSessionFactoryConfig { 

 @Conditional

這是 Spring 4.0 添加的新注解,用來(lái)標(biāo)識(shí)一個(gè) Spring Bean 或者 Configuration 配置文件,當(dāng)滿足指定的條件才開啟配置。

  1. @Bean 
  2. @Conditional({SEEConditional.class}) 
  3. public ServerEndpointExporter serverEndpointExporter (){   
  4.   return new ServerEndpointExporter();   
  5. public class SEEConditional implements Condition { 
  6.  
  7.     @Override 
  8.     public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { 
  9.         String active = context.getEnvironment().getProperty("app.profiles.active") ; 
  10.         return !"prod".equals(active) ; 
  11.     } 
  12.  

 @ConditionalOnBean

當(dāng)容器中有指定的 Bean 才開啟配置。

@ConditionalOnMissingBean

當(dāng)容器中沒有指定的 Bean 才開啟配置。

  1. @ConditionalOnMissingBean({ DataSource.class, XADataSource.class }) 
  2. protected static class EmbeddedConfiguration { 

 @ConditionalOnClass

組合 @Conditional 注解,當(dāng)容器中有指定的 Class 才開啟配置。

  1. @ConditionalOnClass({ RabbitTemplate.class, Channel.class }) 
  2. public class RabbitAutoConfiguration { 
  3. }   

 @ConditionalOnMissingClass

當(dāng)容器中沒有指定的 Class 才開啟配置。

@ConditionalOnWebApplication

當(dāng)前項(xiàng)目類型是 WEB 項(xiàng)目才開啟配置。

當(dāng)前項(xiàng)目有以下 3 種類型。

  1. /** 
  2.  * Any web application will match. 
  3.  */ 
  4. ANY
  5. /** 
  6.  * Only servlet-based web application will match. 
  7.  */ 
  8. SERVLET, 
  9. /** 
  10.  * Only reactive-based web application will match. 
  11.  */ 
  12. REACTIVE 

 @ConditionalOnNotWebApplication

當(dāng)前項(xiàng)目類型不是 WEB 項(xiàng)目才開啟配置。

@ConditionalOnProperty

當(dāng)指定的屬性有指定的值時(shí)才開啟配置。

  1. @Bean 
  2. @ConditionalOnProperty(prefix = "spring.rabbitmq"name = "dynamic", matchIfMissing = true
  3. public AmqpAdmin amqpAdmin(ConnectionFactory connectionFactory) { 
  4.   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 注解方法上面。

  1. @Bean 
  2. @ConfigurationProperties(prefix = DataSourceProperties.PREFIX) 
  3. public DataSource dataSource() { 
  4.      DataSourceBuilder factory = DataSourceBuilder 
  5.                     .create(this.properties.getClassLoader()) 
  6.                     .driverClassName(this.properties.getDriverClassName()) 
  7.                     .url(this.properties.getUrl()).username(this.properties.getUsername()) 
  8.                     .password(this.properties.getPassword()); 
  9.             if (this.properties.getType() != null) { 
  10.                 factory.type(this.properties.getType()); 
  11.             } 
  12.             return factory.build(); 

 @EnableConfigurationProperties

配合 @ConfigurationProperties 注解使用,用來(lái)開啟對(duì) @ConfigurationProperties 注解配置 Bean 的支持。

  1. @Configuration 
  2. @ConditionalOnClass({ DataSource.class, EmbeddedDatabaseType.class }) 
  3. @EnableConfigurationProperties(DataSourceProperties.class) 
  4. public class DataSourceAutoConfiguration { 
  5. }   

 @AutoConfigureAfter

用在自動(dòng)配置類上面,表示該自動(dòng)配置類需要在另外指定的自動(dòng)配置類配置完之后。

如 Mybatis 的自動(dòng)配置類,需要在數(shù)據(jù)源自動(dòng)配置類之后。

  1. @AutoConfigureAfter({ DataSourceAutoConfiguration.class, MybatisLanguageDriverAutoConfiguration.class }) 
  2. public class MybatisAutoConfiguration implements InitializingBean { 
  3. }   

 @AutoConfigureBefore

表示該自動(dòng)配置類需要在另外指定的自動(dòng)配置類配置之前。

@Import

這是 Spring 3.0 添加的新注解,用來(lái)導(dǎo)入一個(gè)或者多個(gè) @Configuration 注解修飾的類,這在 SpringBoot 里面應(yīng)用很多。

  1. @Import(CachingConfigurationSelector.class) 
  2. public @interface EnableCaching { 
  3. }   

 @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)入。

  1. @ImportResource({ "classpath:spring/application-*.xml" }) 
  2. @SpringBootApplication 
  3. public class AppApplication { 
  4. }   

 @RestController

該注解是@ResponseBody + @Controller的組合。返回的內(nèi)容是return 的內(nèi)容,無(wú)法返回jsp或html頁(yè)面等視圖文件。

  1. @RestController 
  2. @RequestMapping("/users"
  3. public class UsersController { 
  4. }   

 @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)求路徑中的占位符的值。

 

責(zé)任編輯:姜華 來(lái)源: 今日頭條
相關(guān)推薦

2021-04-27 07:52:18

跳槽數(shù)據(jù)分析

2020-04-03 18:43:21

大數(shù)據(jù)Hadoop數(shù)據(jù)

2021-05-11 07:39:58

跳槽談薪工作

2020-12-24 15:26:07

Redis數(shù)據(jù)庫(kù)

2024-09-05 09:25:59

SpringUserDAO接口

2024-02-26 08:19:00

WebSpring容器

2020-09-24 10:00:50

SpringBoo

2024-04-28 08:20:52

Controller接口URL

2021-10-25 14:55:38

Linux技巧命令

2021-05-18 08:02:40

面試面試問題職業(yè)規(guī)劃

2021-01-04 08:37:53

動(dòng)態(tài)規(guī)劃DP

2015-10-26 09:19:28

PHP經(jīng)驗(yàn)

2018-10-15 12:42:21

2024-11-07 16:39:42

SpringBoo常用注解Bean

2023-06-30 08:26:24

Java注解Java程序元素

2021-06-04 10:11:07

鴻蒙安卓操作系統(tǒng)

2021-10-12 09:20:02

數(shù)據(jù)庫(kù)SQL腳本

2021-12-07 13:45:38

WOT技術(shù)峰會(huì)技術(shù)

2020-09-17 16:08:29

網(wǎng)絡(luò)安全數(shù)據(jù)技術(shù)

2022-11-11 08:31:39

Java注解注解類
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)