@RefreshScope副作用-自动配置,自动接线

时间:2019-06-10 12:14:55

标签: spring spring-boot spring-security spring-cloud

在Spring Config Server中进行更改后,我正在使用@RefreshScope注释刷新我的bean。效果很好,但在某些情况下有副作用。

第一种情况:我有一个实现PermissionEvaluator(spring-security)的bean CustomPermissionEvaluator,以便覆盖默认实现(DenyAllPermissionEvaluator)。

import org.springframework.security.access.PermissionEvaluator;

@Component
public class CustomPermissionEvaluator implements PermissionEvaluator {

    @Override
    public boolean hasPermission(Authentication authentication, Object targetDomainObject, Object permission) {

如果我用@RefreshScope注释CustomPermissionEvaluator,Spring将不再考虑它,并且将在此自定义实现上选择默认实现。

第二种情况:我有以下bean:

@Bean
@Profile("enable_mongo_ssl")
public MongoClientOptions mongoClientOptions() {
    return getMongoClientOptionsBuilder()
            .build();
    }

如果我用@RefreshScope进行注释,则在构建MongoClient时,MongoAutoConfiguration不会使用它,而是使用不带证书的默认mongoClientOptions。

最后一种情况: 我定义了一个对象映射器:

@Bean
@Primary
public ObjectMapper defaultObjectMapper() {
    return ObjectMapperProvider.defaultObjectMapper();
}

如果使用@RefreshScope对其进行注释,则会出现以下错误:

more than one 'primary' bean found among candidates: [defaultObjectMapper, jacksonObjectMapper]

我想这些示例与@RefreshScope注释导致Bean被代理这一事实有关。 但是,有没有一种干净的方法可以继续使用此注释并使这些功能正常工作?

0 个答案:

没有答案