加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-15 01:22:36 所属栏目:大数据 来源:网络整理
导读:用于所有数据源连接的连接池类.它有一个静态枚举来指示连接类型. class ConnectionPool { public static enum Type { t1,t2,t3; }…} 另一个类没有默认承包商,构造函数将Type作为承包商参数 class Update { public Update(Type type) { this.type = type; }.

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

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);
    }

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读