简体   繁体   中英

How to create Spring Bean (DataSource) with parameter?

Colleagues, i am new in Spring 4. I have a class which call procedure:

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<String, Object> 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;
    }

I need to use dataSource with different input arguments.

My bean in WebServiceConfig (wsc) look like:

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

But when run program i receive next stack trace:

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

When i use bean without "String source" it works fine?

UPDATE

I solved problem by using separate bean for every datasource:

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

When you pass parameters in methods annotated with @Bean every parameter is resolved by the spring container, in your specific case spring can'f find a bean of type string to satisfy the dependency because you haven't set up any (and you shouldn't have! :)).

If you want to inject some parameters, spring provides you with the @Value annotation (this is one of few different ways spring can be helpful to inject properties btw), eg:

public BasicDataSource DataSource(@Value("${your.property.name}") String source)

You'll need to define the property your.property.name inside the application.properties file inside your resource folder like this

your.property.name=my value

nb

you can use @PropertySource in your context configuration file to import properties from wherever you prefer http://docs.spring.io/spring-framework/docs/4.0.x/javadoc-api/org/springframework/context/annotation/PropertySource.html (I've been so spoiled by spring boot that I forgot if spring automatically loads the properties on its own :D)

You can use a @PropertySource("yourFile.properties") on your configuration and get property through environment like this :

    @Configuration
    @PropertySource("yourFile.properties")
    public class YourConfiguration{

        @Autowired
        Environment env

        @Bean
        public BasicDataSource DataSource() {
         String source = env.getProperty("source");
         //your code below
        }
     }

Hope this help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM