title: How to create Spring Bean (DataSource) with parameter?
body: <p>Colleagues, i am new in Spring 4. 
  I have a class which call procedure:</p>
  
  <pre><code>public class ProductDAOImpl implements ProductDAO  {
  
    public Product getProductUsingProc(int PassID, int Amount) {
  
  
          SqlParameterSource sqlParameterSource = new MapSqlParameterSource().addValue("PassID", PassID).addValue("Amount", Amount);
           WebServiceConfig wsc = new WebServiceConfig();
  
          DataSource dataSource = wsc.DataSource("DB1"); 
          SimpleJdbcCall simpleJdbcCall = new SimpleJdbcCall(dataSource).withProcedureName("PRODUCT_CREATE");
  
          Map&lt;String, Object&gt; result = simpleJdbcCall.execute(sqlParameterSource);
  
          Product product = new Product();
  
          product.setPassID(PassID); 
          product.setAmount(Amount);
          product.setReturnValue( (int) result.get("ReturnValue") );
          product.setProductID((int) result.get("ProductID"));
  
          return product;
      }
  </code></pre>
  
  <p>I need to use dataSource with different input arguments.  </p>
  
  <p>My bean in WebServiceConfig (wsc) look like:</p>
  
  <pre><code>@Bean
          public BasicDataSource DataSource(String source) {
               /*some code using input parameter*/
  
              BasicDataSource dataSource = new BasicDataSource();
              dataSource.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
              dataSource.setUrl("jdbc:....");
              dataSource.setUsername("user");
              dataSource.setPassword("pass");
              dataSource.setMaxIdle(10);
              dataSource.setMaxWaitMillis(10000);
              dataSource.setValidationQuery("select 1");
              dataSource.setTestOnBorrow(false);
              dataSource.setTestWhileIdle(true);
              dataSource.setDefaultAutoCommit(true);
              return dataSource;
          }
  </code></pre>
  
  <p>But when run program i receive next stack trace:</p>
  
  <pre><code>Error starting ApplicationContext. To display the auto-configuration report enable debug logging (start with --debug)
  
  
  ERROR: [oct-26 16:46:44,337] springframework.boot.SpringApplication - Application startup failed org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'DataSource' defined in class path resource [com/mayacomp/service/app/WebServiceConfig.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [java.lang. String]: : No qualifying bean of type [java.lang.String] found for dependency: expected at least 1 bean which qualifies as autowire candidate for  this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [java.lang.String] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Depende ncy annotations: {}
          at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749)
          at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:464)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBean Factory.java:1119)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.jav a:1014)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
  
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
          at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
          at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
          at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
          at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
          at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
          at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
          at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
          at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
          at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
          at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
          at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
          at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
          at com.mayacomp.service.app.WsApplication.main(WsApplication.java:12) Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [java.lang.String] found for dependency: e xpected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
          at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.jav a:1301)
          at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047)
          at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
          at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:813)
          at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
          ... 18 more Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'DataSource' defined i n class path resource [com/mayacomp/service/app/WebServiceConfig.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [java.lang.String]: : No qualifying bean of type [java.lang.String] found for dependency: expected at least 1 bean which qualifie s as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefiniti onException: No qualifying bean of type [java.lang.String] found for dependency: expected at least 1 bean which qualifies as autowire candidate f or this dependency. Dependency annotations: {}
          at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749)
          at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:464)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBean Factory.java:1119)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.jav a:1014)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
  
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
          at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
          at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
          at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
          at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
          at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
          at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
          at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
          at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
          at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
          at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
          at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
          at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
          at com.mayacomp.service.app.WsApplication.main(WsApplication.java:12) Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [java.lang.String] found for dependency: e xpected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
          at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.jav a:1301)
          at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047)
          at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
          at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:813)
          at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
          ... 18 more
  </code></pre>
  
  <p>When i use bean without "String source" it works fine?      </p>
  
  <p><strong>UPDATE</strong> </p>
  
  <p>I solved problem by using separate bean for every datasource:</p>
  
  <pre><code>@Bean
      @Primary
      @ConfigurationProperties(prefix="datasource.primary")
          public BasicDataSource DataSource() {
              BasicDataSource dataSource = new BasicDataSource();
              dataSource.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
              dataSource.setUrl("jdbc:sqlserver://");
              dataSource.setUsername("user");
              dataSource.setPassword("pass");
              dataSource.setMaxIdle(10);
              dataSource.setMaxWaitMillis(10000);
              dataSource.setValidationQuery("select 1");
              dataSource.setTestOnBorrow(false);
              dataSource.setTestWhileIdle(true);
              dataSource.setDefaultAutoCommit(true);
              return dataSource;
          }
  
      @Bean
      @ConfigurationProperties(prefix="datasource.secondary")
      public BasicDataSource DataSource1() {
          BasicDataSource DataSource1 = new BasicDataSource();
          DataSource1.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
          DataSource1.setUrl("jdbc:sqlserver://");
          DataSource1.setUsername("user");
          DataSource1.setPassword("pass");
          DataSource1.setMaxIdle(10);
          DataSource1.setMaxWaitMillis(10000);
          DataSource1.setValidationQuery("select 1");
          DataSource1.setTestOnBorrow(false);
          DataSource1.setTestWhileIdle(true);
          DataSource1.setDefaultAutoCommit(true);
          return DataSource1;
      }
  
  
      @Bean
      @ConfigurationProperties(prefix="datasource.secondary")
      public BasicDataSource DataSource2() {
          BasicDataSource DataSource2 = new BasicDataSource();
          DataSource2.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
          DataSource2.setUrl("jdbc:sqlserver://");
          DataSource2.setUsername("user");
          DataSource2.setPassword("pass");
          DataSource2.setMaxIdle(10);
          DataSource2.setMaxWaitMillis(10000);
          DataSource2.setValidationQuery("select 1");
          DataSource2.setTestOnBorrow(false);
          DataSource2.setTestWhileIdle(true);
          DataSource2.setDefaultAutoCommit(true);
          return DataSource2;
      }
  </code></pre>