一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - spring中bean id相同引發(fā)故障的分析與解決

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

2021-01-09 14:28rhwayfunn Java教程

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

前言

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

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

  • 為什么相同bean id的bean會被覆蓋
  • @autowired注解不是按照bytype的方式進(jìn)行注入的嗎

代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
public class userclient {
 
 private userconfiguration configuration;
 
 public userclient(userconfiguration configuration) {
  this.configuration = configuration;
 }
 
 public string getcity() {
  return configuration.getcity();
 }
 
}

beans.xml:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?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:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?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:

?
1
2
3
4
5
6
7
user1.id=1
user1.name=bean1
user1.city=hangzhou
 
user2.id=2
user2.name=bean2
user2.city=shanghai

applition:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@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);
 }
 
}

運(yùn)行程序,你會發(fā)現(xiàn)不管注入的userclient2還是userclient1,輸出的結(jié)果都是shanghai。但是我們想實現(xiàn)的是,注入userclient1的時候輸出的應(yīng)該是hangzhou,注入userclient2的時候輸出的應(yīng)該是shanghai。這也是導(dǎo)致開頭說的問題的源頭所在。要實現(xiàn)這個效果很簡單,userconfiguration換一個名字就可以了。

但是,為什么換個名字就可以了呢,不同spring配置文件相同bean id的bean為什么不會分別創(chuàng)建呢?原因就在于spring 對具有相同bean id的實例做了覆蓋處理。你可以理解為一個map,key是bean id,value就是class,那么當(dāng)兩次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的預(yù)初始化方法
  7. 調(diào)用initializingbean的afterpropertiesset方法
  8. 調(diào)用自定義的初始化方法
  9. 調(diào)用beanpostprocessor的初始化方法
  10. 實例化完畢

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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)知道是因為被覆蓋的導(dǎo)致的,那么怎么體現(xiàn)的呢?遇到解決不了的問題,看源碼往往能得到答案:

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

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

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

@autowired注解實現(xiàn)機(jī)制

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

你確定嗎?不完全正確。

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

1.解析依賴

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

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

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

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ù)屬性名裝配

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

優(yōu)先級定義:

?
1
2
3
4
5
6
7
8
9
10
11
/**
  * 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)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對服務(wù)器之家的支持。

原文鏈接:http://blog.csdn.net/u011116672/article/details/78074246

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲图片一区二区三区 | 成人依依网 | 亚洲天堂男人 | 国产资源一区 | 传说之下羊妈挤羊奶网站 | 牛牛在线观看 | 动漫美女日批 | 国内自拍网红在线自拍综合 | 日本草草视频在线观看 | 好大好硬好湿好紧h | 欧美伊香蕉久久综合类网站 | 成人国产一区二区 | 青青久久精品国产 | 免费毛片 | 504神宫寺奈绪大战黑人 | 日本中文字幕一区二区有码在线 | 青青精品视频 | 大吊小说 | 海绵宝宝第二季全集免费观看 | 教室里老师好紧h | 岛国最新资源网站 | 国内偷拍第一页 | 午夜办公室 | 欧美va免费精品高清在线 | 男人看片网址 | 动漫人物差差差动漫人物免费观看 | 海派甜心完整版在线观看 | 国产免费久久精品44 | 婷婷精品| 楚乔传第二部免费播放电视连续剧 | 亚洲精品国产自在现线最新 | 国产免费久久精品44 | 第一次不是你高清在线观看 | 含羞草传媒网站免费进入欢迎 | eeuss免费快捷| 嫩草影院永久在线播放 | 亲爱的客栈第二季免费观看完整版 | 四色6677最新永久网站 | 日韩大片免费看 | 免费看麻豆视频 | 射西西|