【问题标题】:Change Autowired object at runtime在运行时更改 Autowired 对象
【发布时间】:2016-09-28 08:46:23
【问题描述】:

我有一个接口ClusterHelper 和一个实现该接口的类ZKClusterHelperZooKeeperConfig 是一个类,我们在其中创建 zookeeper 对象的 instance。现在,在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;
}

我在 B 类中自动装配。

@Autowired
ClusterHelper ch;

现在,如果我更改 ZooKeeperConfig 类中某些字段的值。 如何让它反映在 B 类中自动装配的 ClusterHelper 中。

【问题讨论】:

    标签: java spring spring-mvc apache-zookeeper


    【解决方案1】:

    我认为你不需要在这里做动态autowiring。我建议您为其创建单独的服务 bean 并注入,而不是手动创建新的。

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

    【讨论】:

    • 您好,我没有找到您。我应该为什么创建服务 bean?
    • 我的意思是你不需要这样做ZKClusterHelper zch = new ZKClusterHelper();而不是通过new创建bean,你应该为此创建一个单独的bean并注入(使用)它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-09
    • 1970-01-01
    • 1970-01-01
    • 2012-01-20
    • 2011-08-05
    • 1970-01-01
    • 2011-08-04
    相关资源
    最近更新 更多