微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

春季-已定义的bean接受枚举construtor-arg

用于所有数据源连接的连接池类.它有一个静态枚举来指示连接类型.

class ConnectionPool {

   public static enum Type {
     t1,t2,t3;
   }
…
}

一个类没有认承包商,构造函数将Type作为承包商参数

class Update {
   public Update(Type type) {
      this.type = type;
   }
...
}

在applicationContext.xml中,定义一个bean

<bean id="update" class="package.Update">
    <contructor-arg type="package.ConnectionPool.Type">
        <value>Type.t1</value>
    </contructor-arg>
</bean>

但是我得到了

Error creating bean with name 'update' defined in class path resource [applicationContext.xml]: Unsatisfied dependency expressed through constructor argument with index 0 of type [package.ConnectionPools$PoolType]: Ambiguous constructor argument types - did you specify the correct bean references as constructor arguments?
最佳答案
这应该工作:

<bean id="update" class="package.Update">
    <contructor-arg type="package.ConnectionPool.Type">
        <value>t1</value>
    </contructor-arg>
</bean>

甚至:

<bean id="update" class="package.Update">
    <contructor-arg type="package.ConnectionPool.Type" value="t1"/>
</bean>

或我最喜欢的:

@Configuration
public class Config {

    @Bean
    public Update update() {
        return new Update(t1);
    }

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐