Defined bean to accept enum construtor-arg

The connection pool class used for all data source connections. It has a static enumeration to indicate the type of connection.

class ConnectionPool {

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

      

There is no default contractor in the other class, the constructor takes the type as the contractor argument

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

      

The bean is defined in applicationContext.xml

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

      

But I got

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?

      

+3


source to share


1 answer


This should work:

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

      

or even:



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

      

or my favorite:

@Configuration
public class Config {

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

}

      

+3


source







All Articles