1. 程式人生 > >spring bean迴圈引用問題

spring bean迴圈引用問題

在spring容器啟動的過程中報如下錯誤: 2017-07-04 07:09:53.231 WARN [bi-service-recom-server,,,] 1 --- [ main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'recomController': Unsatisfied dependency expressed through field 'recommService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'recommService': Unsatisfied dependency expressed through field 'personalizedABRouteTable'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'ABTestConf': Unsatisfied dependency expressed through field 'personalizedABRouteTable'; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'personalizedABRouteTable': Requested bean is currently in creation: Is there an unresolvable circular reference?

檢查發現並沒有迴圈依賴的bean,再檢視一下最後一個錯誤資訊的類:

@Component
public class MyClass {
       @Autowired
       PersonalizedABRouteTable personalizedABRouteTable;

       @Bean("personalizedABRouteTable")
    public PersonalizedABRouteTable getPersonalizedABRouteTable() {
        PersonalizedABRouteTable table = new PersonalizedABRouteTable();
        table.orderedRuleList = personalizedRouteRules;
        return table;
    }
}

發現了問題所在。spring在類載入的時候,有兩類方式獲取bean,一是通過@autowired等標籤注入裝載到容器的bean;還有一種方式是使用@bean等標籤通知spring容器生成一個指定的bean。

在這裡PersonalizedABRouteTable並不在spring容器中初始化的時候生成,而是使用MyClass的@bean標籤生成,之後注入到類中。

但是這兩種方式儘量不要混用。在上述例子中只被初始化一次不會有問題,一旦另外一個類同樣需要注入PersonalizedABRouteTable,則容器初始化bean的時候就會產生迴圈依賴,另外一個類一直在等待MyClass初始化生成PersonalizedABRouteTable,而MyClass又沒有裝載完成,因此產生了circle reference。

將@Autowired註釋去掉後,問題解決。

從另外一個層面講,將@bean註解去掉,使用@PostConstruct註解,通過applicationContext獲取bean,交給MyClass後,同樣不會迴圈依賴。原理是相通的。