同事们,我是 Spring 4 的新人。 我有一个调用过程的类:
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;
}
我需要使用具有不同输入参数的数据源。
我的 WebServiceConfig (wsc) 中的 bean 如下所示:
@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;
}
但是当运行程序时,我收到下一个堆栈跟踪:
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
当我使用没有“字符串源”的bean时,它工作正常吗?
更新
我通过为每个数据源使用单独的 bean 解决了问题:
@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;
}
最佳答案
当您在用 @Bean 注释的方法中传递参数时,每个参数都由 spring 容器解析,在您的特定情况下,spring 无法找到字符串类型的 bean 来满足依赖关系,因为您还没有设置任何(并且你不应该这样!:))。
如果你想注入(inject)一些参数,spring 为你提供了 @Value 注解(这是 spring 有助于注入(inject)属性的几种不同方式之一),例如:
public BasicDataSource DataSource(@Value("${your.property.name}") String source)
您需要在资源文件夹内的 application.properties 文件中定义属性 your.property.name ,如下所示
your.property.name=我的值
注意
您可以在上下文配置文件中使用@PropertySource从您喜欢的任何地方导入属性http://docs.spring.io/spring-framework/docs/4.0.x/javadoc-api/org/springframework/context/annotation/PropertySource.html (我已经被 spring boot 宠坏了,以至于我忘记了 spring 是否会自动加载自己的属性:D)
关于java - 如何创建带有参数的Spring Bean(数据源)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33347623/