2017-02-24 378 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。