日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
SpringBoot常用注解總結(jié)

[[343644]]

成都創(chuàng)新互聯(lián)于2013年成立,先為精河等服務(wù)建站,精河等地企業(yè),進行企業(yè)商務(wù)咨詢服務(wù)。為精河企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。

一、啟動相關(guān)
1、@SpringBootApplication
SpringBootApplication注解: 

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

在SpirngBoot啟動類里面,都加入了此啟動注解,此注解是個組合注解,三個比較重要的:

@SpringBootConfiguration 繼承至@Configuration,其實兩種功能一致,都是標注該類為配置類
@EnableAutoConfiguration 這個注解是SpirngBoot自動配置的核心所在,通過此注解,能所有符合自動配置條件的bean的定義加載到spring容器中。
@ComponentScan 該注解會掃描當前包及子包下面被納入sping容器管理的類。
注意事項:

一般我們在實際項目中會根據(jù)自己的需要排除一些無需自動配置的類,例如我們會配置自己的數(shù)據(jù)庫,所以就不需要springboot自動注入數(shù)據(jù)源,這可以利用exclude進行排除
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
當需掃描第三方j(luò)ar包時,我們可以通過在配置類中使用@Configuration與@ComponentScan來引入第三方j(luò)ar包下的類
@Configuration@ComponentScan(basePackages = {"com.test.service"}) //引入第三方j(luò)ar包下的類
2、@Import
@Import注解可用來導入一個或者多個 Spring 配置文件,特別是第三方j(luò)ar包下的一些配置類,需要通過Import注解進行加載,代碼如下所示

@Import({KafkaConfig.class, JedisConfig.class}) //引入第三方j(luò)ar包里配置類

二、配置相關(guān)
以一般的的配置類中用到的注解為例

1、@Configuration

@Configuration專門用來標注配置類,它一般會配合
2、@Bean

使用@Bean注解拿到配置返回相關(guān)實例,并放入sping容器中統(tǒng)一管理
3、@PropertySource

目的是加載指定路徑下的屬性文件
4、@Value

配合@PropertySource注解使用,指定該字段對應(yīng)的配置文件中的內(nèi)容
5、@Order

利用@Order控制配置類的加載順序
結(jié)合以上注解對kafka進行配置示例代碼如下

 
 
 
  1. @Configuration 
  2. @PropertySource("classpath:spring-kafka.properties") 
  3. @Order(2) 
  4. public class KafkaConfig { 
  5.     @Value("${spring.kafka.bootstrap-servers}") 
  6.     private String bootstrapServers; 
  7.     @Bean 
  8.     public KafkaListenerContainerFactory> kafkaListenerContainerFactory() { 
  9.         ConcurrentKafkaListenerContainerFactory factory = new ConcurrentKafkaListenerContainerFactory(); 
  10.         factory.setConsumerFactory(consumerFactory());        factory.setConcurrency(1); 
  11.         // factory .getContainerProperties().setPollTimeout(1000); 
  12.         return factory; 
  13.     } 

三、構(gòu)造相關(guān)
1、@Service

用于標注服務(wù)層,主要用來進行業(yè)務(wù)的邏輯處理
2、@Repository

用于標注持久層,主要用來進行數(shù)據(jù)庫相關(guān)操作
3、@Component

一個通用的注解,可以注解各種組件,就是說當我們需要注入sping容器中bean類沒有明確分類時(不屬于@service、@Repository等的時候),我們就可以使用@Component來標注這個類。
4、@Scope

spring容器管理bean默認是單例模式,如果你需要使用多例模式可以通過@Scope("prototype")注解來實現(xiàn)。
5、@Autowired

這個就很簡單了,用于Spring容器中Bean類實例的注入
6、@PostConstruct

在Bean初始化之后(構(gòu)造方法和@Autowired之后)執(zhí)行指定操作。如果在項目中有些操作需要在Bean類構(gòu)造后執(zhí)行,可以使用@PostConstruct注解,實例代碼如下

 
 
 
  1. @RestController 
  2. @RequestMapping("/api") 
  3. public class ApiController extends BaseController {    @PostMapping("/login") 
  4.     public  User  login(@RequestBody User user){ 
  5.            //代碼 
  6.      } 
  7.     @GetMapping("/getUser") 
  8.     public  User  getUser(@RequestParam String userName, @RequestParam String userPhone){ 
  9.            //代碼 
  10.      } 

 

 


分享文章:SpringBoot常用注解總結(jié)
標題URL:http://m.5511xx.com/article/dhsscdg.html