首页 > 解决方案 > 有没有办法在春天重新加载自动装配的实例或替换自动装配的行为

问题描述

目前我有一个在 @Configuration 中创建的 bean,它从 web 下载 json 文档并创建一个模型对象。使用这个 bean(自动装配),许多其他 bean 在启动时被初始化

每当网络中的 json 文档发生更改时,我需要一种重新加载 bean 的方法。

最好的方法是什么?

代码:

@Configuration
@ComponentScan(basePackages = "com.wellmanage.prism")
public class PrismConfig {

...
@Bean
public Model model(@Qualifier("prismRestTemplate") RestTemplate restTemplate) {

    LOG.info("model()");
    MetadataReader metadataReader = new MetadataReader();
    String prismFormatJson = null;
    if (!isHasLatestTransformedJson()) {
        prismFormatJson = metadataReader.transformToPrismJson(restTemplate, environment);
        setLastGoodPrismConfiguration(prismFormatJson);
    } else {
        prismFormatJson = getLastGoodPrismConfiguration();
    }
    if (model != null) {
        return model;
    } else {
        return metadataReader.createModelForPrism(prismFormatJson);
    }
}

}

@Configuration
@ComponentScan(basePackages = "com.wellmanage.prism")
public class PrismDataSourceConfig implements DataSourceConfig {

private final Logger LOG = LoggerFactory.getLogger(PrismDataSourceConfig.class);

@Autowired
private Environment environment;

@Autowired
private Model model;

@Primary
@Bean(name = "itdb_dataSource")
public DataSource getDataSource() {

    LOG.info("getDataSource()");
    return getDataSource("itdb");
}

@Bean(name = "dataSourceMap")
public Map<String, DataSource> getDataSourceMap() {

    LOG.info("getDataSourceMap()");

    Map<String, DataSource> dataSourceMap = Maps.newHashMap();
    getDatabases().forEach((name, database) -> {
        Endpoint endpoint = getEndpoint(name);
        DataSource dataSource = createDataSource(endpoint);
        dataSourceMap.put(name, dataSource);
    });

    return dataSourceMap;
}

@Bean(name = "jdbcTemplateMap")
public Map<String, JdbcTemplate> getJdbcTemplateMap() {

    LOG.info("getDataSource()");

    Map<String, JdbcTemplate> jdbcTemplateMap = Maps.newHashMap();
    getDataSourceMap().forEach((name, datasource) -> {
        JdbcTemplate jdbcTemplate = new JdbcTemplate(datasource);
        jdbcTemplateMap.put(name, jdbcTemplate);
    });

    return jdbcTemplateMap;
}

@Override
public Environment getEnvironment() {

    return environment;
}

@Override
public Model getModel() {

    return model;
}

}

标签: javaspringautowired

解决方案


你的方法是非常错误的。自动装配用于在启动时连接依赖项。(现在实际上不鼓励使用构造函数参数注入。)

您可能需要的是@Service从远程服务中检索数据模型。然后,您将此服务注入需要它来获取模型的类中。

然后,您还可以使用 EhCache 之类的缓存@Cacheable并向您的方法添加注释,这样您就不会在其他类每次需要模型时都从远程源获取模型。(您可以配置您ehcache.xml希望缓存在刷新数据之前存在多长时间)。

@Service
public class ModelService {

  private final RestTemplate restTemplate;

  public ModelService(RestTemplate restTemplate) {
    this.restTemplate = restTemplate;
  }

  @Cacheable(value = "model", key = "#root.methodName")
  public Model getModel() {

    MetadataReader metadataReader = new MetadataReader();
    String prismFormatJson = null;
    if (!isHasLatestTransformedJson()) {
        prismFormatJson = metadataReader.transformToPrismJson(restTemplate, environment);
        setLastGoodPrismConfiguration(prismFormatJson);
    } else {
        prismFormatJson = getLastGoodPrismConfiguration();
    }
    if (model != null) {
        return model;
    } else {
        return metadataReader.createModelForPrism(prismFormatJson);
    }
  }

  //... the rest of the code
}

这里我们将缓存配置为 10 分钟后过期:

<config
  xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
  xmlns='http://www.ehcache.org/v3'
  xmlns:jsr107='http://www.ehcache.org/v3/jsr107'
  xsi:schemaLocation="
  http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.0.xsd
  http://www.ehcache.org/v3/jsr107 http://www.ehcache.org/schema/ehcache-107-ext-3.0.xsd">

  <service>
    <jsr107:defaults>
      <jsr107:cache name="model" template="model-cache"/>
    </jsr107:defaults>
  </service>

  <cache-template name="model-cache">
    <expiry>
      <ttl unit="minutes">10</ttl>
    </expiry>
  </cache-template>
</config>

推荐阅读