2017-02-24 24 views
0

配置變化我有一個@Configuration類,如下所示:刷新一個Bean而不@RefreshScope

@Configuration 
public class SampleKieConfiguration { 

    @Bean 
    public KieServices kieServices() { 
     return KieServices.Factory.get(); 
    } 

    @Bean 
    public KieContainer kieContainer() { 

     ReleaseId releaseId = kieServices().newReleaseId("groupId", "artifactId", "version"); 
     KieContainer kieContainer = kieServices().newKieContainer(releaseId); 
     kieServices().newKieScanner(kieContainer).start(10_000); 
     return kieContainer; 
    } 

    @Bean 
    public KieBase kieBase() { 

     KieBaseConfiguration kieBaseConfiguration = kieServices().newKieBaseConfiguration(); 
     kieBaseConfiguration.setOption(EqualityBehaviorOption.EQUALITY); 
     kieBaseConfiguration.setOption(EventProcessingOption.CLOUD); 
     return kieContainer().newKieBase(kieBaseConfiguration); 
    } 
} 

kieServices().newKieScanner(kieContainer).start(10_000);線基本上民調遠程Maven倉庫,並刷新kieContainer對象每隔10秒,如果有一個新的工件。

,並在我的上層(如服務層)的地方,我有:

@Service 
@RefreshScope 
public class SampleService { 

    @Autowired 
    private KieBase kBase; 

} 

kBase對象不刷新(至少不能以新的kieContainer對象)據我所見,當我撥打/refresh端點時。我沒有集中配置,當我撥打/refresh時,我收到了一條警告。我想要實現的是在每次更新kieContainer時都會有一個新的kBase對象。我怎樣才能做到這一點?謝謝!

回答

0

刷新不會遍歷層次結構。它只是清除緩存,並在下一個引用(通過它創建的代理)上重新創建bean。在你的情況下,因爲KieBase不是@RefreshScope,它不會被重新創建。因此,將@RefreshScope添加到KieBase聲明中。如果SampleService不需要重新創建,請刪除@RefreshScope註釋。

+0

謝謝,感謝答案。但現在我得到了'java.lang.ClassCastException:$ Proxy11不能轉換爲some.package.KnowledgeBaseImpl'。有什麼想法嗎? –

-1

的文檔狀態:

@RefreshScope works (technically) on an @Configuration class, but it might lead to surprising behaviour: 
e.g. it does not mean that all the @Beans defined in that class are themselves @RefreshScope. 
Specifically, anything that depends on those beans cannot rely on them being updated when a refresh is initiated, 
unless it is itself in @RefreshScope (in which it will be rebuilt on a refresh and its dependencies re-injected, 
at which point they will be re-initialized from the refreshed @Configuration). 

所以我想你不得不直接標註一個@RefreshScope也對KieBase @Bean。

相關問題