最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

spring中bean id相同引發(fā)故障的分析與解決

 更新時間:2017年09月24日 12:00:11   作者:rhwayfunn  
最近在工作中遇到了關(guān)于bean id相同引發(fā)故障的問題,通過查找相關(guān)資料終于解決了,下面這篇文章主要給大家介紹了因為spring中bean id相同引發(fā)故障的分析與解決方法,需要的朋友可以參考借鑒,下面來一起看看吧。

前言

最近因為同事bean配置的問題導致生產(chǎn)環(huán)境往錯誤的redis實例寫入大量的數(shù)據(jù),差點搞掛redis。經(jīng)過快速的問題定位,發(fā)現(xiàn)是同事新增一個redis配置文件,并且配置的RedisSentinelConfiguration的id是一樣的,然后在使用@Autowired注入bean的時候因為spring bean覆蓋的機制導致讀取的redis配置不是原來的。

總結(jié)起來,有兩點問題:

  • 為什么相同bean id的bean會被覆蓋
  • @Autowired注解不是按照byType的方式進行注入的嗎

代碼如下:

public class UserConfiguration {

 private int id;

 private String name;

 private String city;

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public String getCity() {
  return city;
 }

 public void setCity(String city) {
  this.city = city;
 }
}

UserClient:

public class UserClient {

 private UserConfiguration configuration;

 public UserClient(UserConfiguration configuration) {
  this.configuration = configuration;
 }

 public String getCity() {
  return configuration.getCity();
 }

}

beans.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans.xsd">

 <bean id="userConfiguration" class="com.rhwayfun.springboot.starter.rest.UserConfiguration">
  <property name="id" value="${user1.id}"/>
  <property name="name" value="${user1.name}"/>
  <property name="city" value="${user1.city}"/>
 </bean>

 <bean id="userClient" class="com.rhwayfun.springboot.starter.rest.UserClient" autowire="byName">
  <constructor-arg ref="userConfiguration"/>
 </bean>

</beans>

beans2.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans.xsd">

 <bean id="userConfiguration" class="com.rhwayfun.springboot.starter.rest.UserConfiguration">
  <property name="id" value="${user2.id}"/>
  <property name="name" value="${user2.name}"/>
  <property name="city" value="${user2.city}"/>
 </bean>

 <bean id="userClient2" class="com.rhwayfun.springboot.starter.rest.UserClient">
  <constructor-arg ref="userConfiguration"/>
 </bean>

</beans>

application.properties:

user1.id=1
user1.name=bean1
user1.city=Hangzhou

user2.id=2
user2.name=bean2
user2.city=Shanghai

Applition:

@SpringBootApplication
public class Application{

 @Autowired
 UserClient userClient2;

 @PostConstruct
 public void init() {
  String city = userClient2.getCity();
  System.out.println(city);
 }

 public static void main(String[] args) throws InterruptedException {
  SpringApplication.run(Application.class, args);
  Thread.sleep(Long.MAX_VALUE);
 }

}

運行程序,你會發(fā)現(xiàn)不管注入的userClient2還是userClient1,輸出的結(jié)果都是Shanghai。但是我們想實現(xiàn)的是,注入userClient1的時候輸出的應該是Hangzhou,注入userClient2的時候輸出的應該是Shanghai。這也是導致開頭說的問題的源頭所在。要實現(xiàn)這個效果很簡單,UserConfiguration換一個名字就可以了。

但是,為什么換個名字就可以了呢,不同spring配置文件相同bean id的bean為什么不會分別創(chuàng)建呢?原因就在于spring 對具有相同bean id的實例做了覆蓋處理。你可以理解為一個Map,key是bean id,value就是class,那么當兩次put相同id的bean的時候自然就被覆蓋了。

我們先回憶下bean的生命周期:

  1. 實例化
  2. 填充屬性
  3. 調(diào)用BeanNameAware的setBeanName方法
  4. 調(diào)用BeanFactoryAware的setBeanFactory方法
  5. 調(diào)用ApplicationContextAware的setApplicationContext方法
  6. 調(diào)用BeanPostProcessor的預初始化方法
  7. 調(diào)用InitializingBean的afterPropertiesSet方法
  8. 調(diào)用自定義的初始化方法
  9. 調(diào)用BeanPostProcessor的初始化方法
  10. 實例化完畢

問題出在注冊bean定義的時候,我們可以控制臺看到以下輸出

Overriding bean definition for bean 'userConfiguration' with a 
different definition: replacing [Generic bean: class 
[com.rhwayfun.springboot.starter.rest.UserConfiguration]; scope=; 
abstract=false; lazyInit=false; autowireMode=0; 
dependencyCheck=0; autowireCandidate=true; primary=false; 
factoryBeanName=null; factoryMethodName=null; initMethodName=null; 
destroyMethodName=null; 
defined in file [/Users/chubin/IdeaProjects/spring-boot-learning-examples/
spring-boot-starter-rest/target/classes/beans.xml]] with 
[Generic bean: class [com.rhwayfun.springboot.starter.rest.UserConfiguration]; 
scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; 
autowireCandidate=true; primary=false; factoryBeanName=null; 
factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in 
file [/Users/chubin/IdeaProjects/spring-boot-learning-examples
/spring-boot-starter-rest/target/classes/beans2.xml]]

就是說beans.xml中配置的UserConfiguration被beans2.xml配置的UserConfiguration實例覆蓋了。那么自然我們得到的結(jié)果是Shanghai了。

spring bean覆蓋

經(jīng)過上面的分析,我們已經(jīng)知道是因為被覆蓋的導致的,那么怎么體現(xiàn)的呢?遇到解決不了的問題,看源碼往往能得到答案:

spring bean覆蓋

spring bean覆蓋2

這段代碼的邏輯就是,如果不允許具有相同bean id的實例存在就拋出異常,而這個值默認是true,也就是允許存在相同的bean id定義。

@Autowired注解實現(xiàn)機制

bean覆蓋的問題解決了,那么還有一個問題,為什么使用@Autowired注入UserClient沒有報錯呢,明明配置了兩個類型的bean啊。@Autowired不是按照byType注入的嗎。

你確定嗎?不完全正確。

因為@Autowired是spring提供的注解,我們可以看到是如何注入的代碼,在AutowiredAnnotationBeanPostProcessor.AutowiredMethodElement.inject()方法中。

1.解析依賴

inject

2.獲取候選bean、決定最終被被注入的最優(yōu)bean

bean1

3.最優(yōu)bean的決策過程:1)判斷時候有@Primary注解;2)如果沒有,得到最高優(yōu)先級的bean,也就是是否有實現(xiàn)了org.springframework.core.Ordered接口的bean(優(yōu)先級比較,可以通過注解@Order(0)指定,數(shù)字越小,優(yōu)先級越高);3)如果仍然沒有,則根據(jù)屬性名裝配

bean2

優(yōu)先級定義:

/**
  * Useful constant for the highest precedence value.
  * @see java.lang.Integer#MIN_VALUE
  */
 int HIGHEST_PRECEDENCE = Integer.MIN_VALUE;

 /**
  * Useful constant for the lowest precedence value.
  * @see java.lang.Integer#MAX_VALUE
  */
 int LOWEST_PRECEDENCE = Integer.MAX_VALUE;

至此,我們就能理解為什么@Autowired能夠通過屬性名注入不同的bean了。

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

  • java題解Leetcode 8字符串轉(zhuǎn)換整數(shù)

    java題解Leetcode 8字符串轉(zhuǎn)換整數(shù)

    這篇文章主要為大家介紹了java題解Leetcode 8字符串轉(zhuǎn)換整數(shù)實現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-06-06
  • Java全面深入探究SpringBoot攔截器與文件上傳

    Java全面深入探究SpringBoot攔截器與文件上傳

    攔截器對使用SpringMvc、Struts的開發(fā)人員來說特別熟悉,因為你只要想去做好一個項目必然會用到它,文件上傳是一個很常見的功能。在項目開發(fā)過程中,我們通常都會使用一些成熟的上傳組件來實現(xiàn)對應的功能
    2022-05-05
  • SpringBoot項目中Maven剔除無用Jar引用的最佳實踐

    SpringBoot項目中Maven剔除無用Jar引用的最佳實踐

    在?Spring?Boot?項目開發(fā)中,Maven?是最常用的構(gòu)建工具之一,通過?Maven,我們可以輕松地管理項目所需的依賴,而,隨著項目的復雜化,無用的?Jar?包引用可能會逐漸增多,本文旨在詳細解析如何在?Spring?Boot?項目中剔除無用的?Jar?引用,需要的朋友可以參考下
    2025-01-01
  • javaweb頁面附件、圖片下載及打開(實現(xiàn)方法)

    javaweb頁面附件、圖片下載及打開(實現(xiàn)方法)

    下面小編就為大家?guī)硪黄猨avaweb頁面附件、圖片下載及打開(實現(xiàn)方法)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • Java使用Random類生成隨機數(shù)示例

    Java使用Random類生成隨機數(shù)示例

    這篇文章主要介紹了Java使用Random類生成隨機數(shù),結(jié)合實例形式分析了java基于Random類生成隨機數(shù)與遍歷輸出相關(guān)操作技巧,需要的朋友可以參考下
    2019-07-07
  • JWT 設置token過期時間無效的解決

    JWT 設置token過期時間無效的解決

    這篇文章主要介紹了JWT 設置token過期時間無效的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • 在idea中創(chuàng)建SpringBoot模塊的兩種方式

    在idea中創(chuàng)建SpringBoot模塊的兩種方式

    這篇文章主要介紹了在idea中創(chuàng)建一個SpringBoot模塊,本文給大家分享兩種方式,每種方式分步驟給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-05-05
  • Java后綴數(shù)組之求sa數(shù)組的實例代碼

    Java后綴數(shù)組之求sa數(shù)組的實例代碼

    后綴數(shù)組就是一個字符串所有后綴大小排序后的一個集合,然后我們根據(jù)后綴數(shù)組的一些性質(zhì)就可以實現(xiàn)各種需求。這篇文章主要介紹了Java后綴數(shù)組-求sa數(shù)組,需要的朋友可以參考下
    2018-04-04
  • Java 圖片壓縮實現(xiàn)思路及代碼

    Java 圖片壓縮實現(xiàn)思路及代碼

    本文為大家詳細介紹下圖片壓縮的具體實現(xiàn)思路及java代碼,想學習的各位可以參考下哈,希望對大家有所幫助
    2013-07-07
  • Spring Boot2.3 新特性分層JAR的使用

    Spring Boot2.3 新特性分層JAR的使用

    這篇文章主要介紹了Spring Boot2.3 新特性分層JAR的使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-06-06

最新評論

崇州市| 高雄县| 余江县| 乌兰浩特市| 浦城县| 靖西县| 平乡县| 青冈县| 清镇市| 墨脱县| 陆河县| 额敏县| 台东县| 洛阳市| 那曲县| 怀仁县| 吉木萨尔县| 泰兴市| 牙克石市| 乐山市| 赤水市| 古浪县| 康平县| 白山市| 兴城市| 中西区| 晋中市| 彰化市| 治县。| 浦县| 丰原市| 尼玛县| 孝昌县| 襄汾县| 东方市| 瓦房店市| 合作市| 武平县| 银川市| 济南市| 临西县|