java 找不到类型为 [javax.sql.DataSource] 的合格 bean
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31347959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
No qualifying bean of type [javax.sql.DataSource] found
提问by rray
I am struggling in understanding why this error is happening. I am porting a tutorial over to the latest version of Spring, Hibernate, and WildFly. I am running from the commandline building and testing the application using Maven. I am getting the below error:
我正在努力理解为什么会发生这个错误。我正在将教程移植到最新版本的 Spring、Hibernate 和 WildFly。我正在使用 Maven 从命令行构建和测试应用程序运行。我收到以下错误:
Jul 10, 2015 2:18:03 PM org.springframework.test.context.TestContextManager prepareTestInstance SEVERE: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@523884b2] to prepare test instance [com.russ.home.test.dao.CompanyDaoTest@131774fe] org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.russ.home.test.dao.CompanyDaoTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests.setDataSource(javax.sql.DataSource); nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
2015 年 7 月 10 日下午 2:18:03 org.springframework.test.context.TestContextManager prepareTestInstance 严重:在允许 TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@523884b2] 准备测试实例时捕获异常 [com.russ .home.test.dao.CompanyDaoTest@131774fe] org.springframework.beans.factory.BeanCreationException:创建名为“com.russ.home.test.dao.CompanyDaoTest”的 bean 时出错:自动装配依赖项的注入失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:无法自动装配方法:public void org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests.setDataSource(javax.sql.DataSource); 嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到类型为 [javax.sql.DataSource] 的符合条件的依赖项:预期至少有 1 个符合此依赖项的自动装配候选者的 bean。依赖注释:{}
This is my testing application contextfile:
这是我的测试应用程序上下文文件:
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="classpath:jdbc.properties" />
<bean id="tttDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.url}"
p:username="${jdbc.username}"
p:password="${jdbc.password}"
/>
<bean id="jpaVendorAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
p:showSql="true"
p:databasePlatform="org.hibernate.dialect.MySQLDialect" />
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:persistenceUnitName="ttt-jpa"
p:dataSource-ref="tttDataSource"
p:jpaVendorAdapter-ref="jpaVendorAdapter"
p:persistenceXmlLocation="classpath*:META-INF/test-persistence.xml"
/>
<!-- Transaction manager for a single JPA EntityManagerFactory (alternative to JTA) -->
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager"
p:dataSource-ref="tttDataSource"
p:entityManagerFactory-ref="entityManagerFactory"/>
<!-- checks for annotated configured beans -->
<context:annotation-config/>
This is the Abstract Test Class
这是抽象测试类
@WebAppConfiguration
@ContextConfiguration
({
"classpath*: /testingContext.xml",
})
public abstract class AbstractDaoForTesting extends AbstractTransactionalJUnit4SpringContextTests {
protected final Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired(required = true)
protected CompanyDao companyDao;
@Autowired(required = true)
protected ProjectDao projectDao;
@Autowired(required = true)
protected TaskDao taskDao;
@Autowired(required = true)
protected UserDao userDao;
@Autowired(required = true)
protected TaskLogDao taskLogDao;
}
This is the CompanyDAOTestClass.
这是CompanyDAOTest类。
@RunWith(SpringJUnit4ClassRunner.class)
public class CompanyDaoTest extends AbstractDaoForTesting {
public CompanyDaoTest(){}
/**
* Test case for the find(id) method of the CompanyDao implementation
* @throws Exception
*/
@Test
public void testFind() throws Exception {
logger.debug("\nSTARTED testFind()\n");
List<Company> allItems = companyDao.findAll();
assertTrue(allItems.size() > 0);
// get the first item in the list
Company c1 = allItems.get(0);
int id = c1.getId();
Company c2 = companyDao.find(id);
assertTrue(c1.equals(c2));
logger.debug("\nFINISHED testFind()\n");
}
Any suggestions would be greatly appreciated.
任何建议将不胜感激。
Russ
拉斯
回答by rray
After comparing others application context files and re-reading Spring documentation, I learned that I was missing several things. I started over and created this new application context and moved pass this error.
在比较了其他应用程序上下文文件并重新阅读 Spring 文档后,我发现我遗漏了一些东西。我重新开始并创建了这个新的应用程序上下文并移动了这个错误。
<!-- Activates various annotations to be detected in bean classes for ex @Autowired -->
<context:annotation-config/>
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="classpath:jdbc.properties" />
<bean id="tttDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.url}"
p:username="${jdbc.username}"
p:password="${jdbc.password}"
/>
<bean id="jpaVendorAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
p:showSql="true"
p:databasePlatform="org.hibernate.dialect.MySQLDialect" />
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:persistenceUnitName="ttt-jpa"
p:dataSource-ref="tttDataSource"
p:jpaVendorAdapter-ref="jpaVendorAdapter"
p:persistenceXmlLocation="classpath*:META-INF/persistence.xml"
/>
<!-- Transaction manager for JTA -->
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager"
p:dataSource-ref="tttDataSource"
p:entityManagerFactory-ref="entityManagerFactory"/>
<!-- enable the configuration of transactional behavior based on annotations -->
<tx:annotation-driven />
<!-- Scan for Repository/Service annotations -->
<context:component-scan base-package="com.russ.home.dao"/>
<context:component-scan base-package="com.russ.home.service"/>