在运行时更改自动装配对象

Change Autowired object at runtime

我有一个接口 ClusterHelper 和一个实现该接口的 class ZKClusterHelperZooKeeperConfig 是一个 class,我们在其中创建 zookeeper 对象的实例 。现在,在 ClusterHelperFactory 中我们有:

@Autowired
ConfigAccessor configAccessor

@Bean
ClusterHelper clusterHelper(){

    logger.info("Returning zookeeper cluster herper");
    ZKClusterHelper zch = new ZKClusterHelper();
    zch.setZookeeper((ZooKeeperConfig)configAccessor);
    ch = zch;

    return ch;
}

我在 class B.

中自动装配
@Autowired
ClusterHelper ch;

现在,如果我更改 ZooKeeperConfig class 中某些字段的值。 我如何让它反映在 class B.

中自动装配的 ClusterHelper

我认为您不需要在此处进行动态 autowiring。我建议您为它创建单独的服务 bean,然后 inject 而不是手动创建新服务 bean。

考虑到默认情况下 spring bean 的作用域为 singleton.