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

Spring依賴注入@Autowried的這些功能你都知道嗎?

開發(fā) 前端
@Autowried注解首先根據(jù)byType注入,如果有多個的情況會按照byName注入,如果沒有符合的名稱那么系統(tǒng)將會報錯。結(jié)合@Qualifier限定注入的Bean。

[[413925]]

環(huán)境:spring5.2.15

@Autowried注解首先根據(jù)byType注入,如果有多個的情況會按照byName注入,如果沒有符合的名稱那么系統(tǒng)將會報錯。結(jié)合@Qualifier限定注入的Bean。

功能1

  • As of Spring Framework 4.3, an @Autowired annotation on such a constructor is no longer necessary if the target bean defines only one constructor to begin with. However, if several constructors are available and there is no primary/default constructor, at least one of the constructors must be annotated with @Autowired in order to instruct the container which one to use.

大意: 從spring4.3開始,如果目標(biāo)bean只定義了一個構(gòu)造函數(shù),則不再需要在這樣的構(gòu)造函數(shù)上使用@Autowired注釋。但是,如果有多個構(gòu)造函數(shù)可用,并且沒有主/默認(rèn)構(gòu)造函數(shù),則必須至少用@Autowired對其中一個構(gòu)造函數(shù)進(jìn)行注釋,以便指示容器使用哪個構(gòu)造函數(shù)。

示例:

  1. public class SimpleMovieLister { 
  2.   private MovieFinder movieFinder; 
  3.  
  4.   @Autowired 
  5.   public void setMovieFinder(MovieFinder movieFinder) { 
  6.     this.movieFinder = movieFinder; 
  7.   } 

功能2

@Autowired可以將注釋應(yīng)用于具有任意名稱和多個參數(shù)的方法

示例:

  1. public class MovieRecommender { 
  2.  
  3.   private MovieCatalog movieCatalog; 
  4.  
  5.   private CustomerPreferenceDao customerPreferenceDao; 
  6.  
  7.   @Autowired 
  8.   public void prepare(MovieCatalog movieCatalog,CustomerPreferenceDao customerPreferenceDao) { 
  9.     this.movieCatalog = movieCatalog; 
  10.     this.customerPreferenceDao = customerPreferenceDao; 
  11.   } 

功能3

可以將@Autowired應(yīng)用于字段,甚至可以將其與構(gòu)造函數(shù)混合使用

  1. public class MovieRecommender { 
  2.   private final CustomerPreferenceDao customerPreferenceDao; 
  3.  
  4.   @Autowired 
  5.   private MovieCatalog movieCatalog; 
  6.  
  7.   @Autowired 
  8.   public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) { 
  9.     this.customerPreferenceDao = customerPreferenceDao; 
  10.   } 

功能4

可以通過將@Autowired注釋添加到需要該類型數(shù)組的字段或方法上

  1. public class MovieRecommender { 
  2.  
  3.   @Autowired 
  4.   private MovieCatalog[] movieCatalogs; 
  5.  

功能5

可以通過將@Autowired注釋添加到需要該類型集合類的字段或方法上

  1. public class MovieRecommender { 
  2.  
  3.   private Set<MovieCatalog> movieCatalogs; 
  4.  
  5.   @Autowired 
  6.   public void setMovieCatalogs(Set<MovieCatalog> movieCatalogs) { 
  7.     this.movieCatalogs = movieCatalogs; 
  8.   } 

注意:如果你希望數(shù)組或list列表注入的bean具有順序,那么你可以對bean實(shí)現(xiàn)Ordered接口或者是使用@Order注解或者標(biāo)準(zhǔn)的 @Priority(JavaEE) 注解。上面的Set不具有順序性

功能6

可以通過將@Autowired注釋添加到Map集合的字段或方法上

  1. public class MovieRecommender { 
  2.  
  3.   private Map<String, MovieCatalog> movieCatalogs; 
  4.  
  5.   @Autowired 
  6.   public void setMovieCatalogs(Map<String, MovieCatalog> movieCatalogs) { 
  7.     this.movieCatalogs = movieCatalogs; 
  8.   } 

這里的key為Bean的名稱。

注意:對于聲明的數(shù)組、集合或Map集合,至少需要一個匹配元素,否則會錯誤。

默認(rèn)情況下使用@Autowired注解意味著是必須具有相應(yīng)的bean存在,否則程序報錯??梢酝ㄟ^如下方式改變默認(rèn)行為

  1. public class SimpleMovieLister { 
  2.   private MovieFinder movieFinder; 
  3.   @Autowired(required = false
  4.   public void setMovieFinder(MovieFinder movieFinder) { 
  5.     this.movieFinder = movieFinder; 
  6.   } 

注意:如果這里沒有合適的MovieFinder類型的Bean那么這里的setter方法都不會執(zhí)行。如果這里是通過構(gòu)造函數(shù)注入,那么即便使用了required = false程序還是會報錯。

功能7

可以通過Java 8的Java.util.Optional來表示特定依賴項(xiàng)的非必需性質(zhì)

  1. public class SimpleMovieLister { 
  2.   @Autowired 
  3.   public void setMovieFinder(Optional<MovieFinder> movieFinder) { 
  4.   } 

功能8

使用 @Nullable 注解標(biāo)準(zhǔn)非必須依賴項(xiàng)

  1. @Autowired 
  2. public void setMovieFinder(@Nullable MovieFinder movieFinder) { 
  3.   System.out.println("-----movie") ; 
  4.   this.movieFinder = movieFinder; 

功能9

@Autowired可以用于注入 BeanFactory, ApplicationContext, Environment, ResourceLoader, ApplicationEventPublisher, and MessageSource

示例:

  1. @Autowired 
  2. private ApplicationContext context; 

注意:

  • The @Autowired, @Inject, @Value, and @Resource annotations are handled by Spring BeanPostProcessor implementations. This means that you cannot apply these annotations within your own BeanPostProcessor or BeanFactoryPostProcessor types (if any). These types must be 'wired up' explicitly by using XML or a Spring @Bean method.

大意:你不能直接在自定義的BeanPostProcessor或BeanFactoryPostProcessor內(nèi)使用@Autowired,@Inject,@Value,@Resource注解;必須通過XML或者@Bean 方法的形式進(jìn)行使用;如果這樣用可能會出現(xiàn)各種奇葩問題,你只需知道不要這么用即可。

 

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

2019-02-12 11:15:15

Spring設(shè)計(jì)模式Java

2024-04-28 08:20:52

Controller接口URL

2021-08-05 18:21:29

Autowired代碼spring

2023-02-15 08:12:19

http超時過濾器

2021-04-10 07:04:00

WPS技巧辦公軟件

2016-01-11 09:48:07

2016-03-18 19:03:35

認(rèn)知計(jì)算IBM

2022-11-10 09:00:41

2019-07-08 10:18:38

MPLSIP數(shù)據(jù)

2023-08-30 07:39:16

PawSQL數(shù)據(jù)庫

2018-05-15 08:27:20

Scikit-lear機(jī)器學(xué)習(xí)Python

2024-03-26 10:10:45

JavaScript操作符操作表達(dá)式

2020-05-27 11:30:54

Chrome DevT前端命令

2022-09-07 09:01:14

JS操作符運(yùn)算符

2023-04-28 12:37:59

Spring@Bean使用方式

2023-04-23 09:50:50

@BeanSpring

2018-04-24 15:40:39

無線路由器無線網(wǎng)絡(luò)上網(wǎng)

2022-05-02 09:17:41

Edge瀏覽器微軟

2020-10-28 11:20:55

vue項(xiàng)目技

2023-08-29 09:31:01

Scrapy網(wǎng)頁爬蟲
點(diǎn)贊
收藏

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