我正在努力理解为什么会发生这种错误。我正在将教程移植到最新版本的Spring,Hibernate和WildFly。我从命令行构建运行并使用Maven测试应用程序。我收到以下错误:
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]的限定bean:期望至少有一个bean可以作为此依赖项的autowire候选者。依赖注释:{}
这是我的测试应用程序上下文文件:
<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/>
这是抽象测试类
@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;
}
这是 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");
}
任何建议都将不胜感激。
拉斯
答案 0 :(得分:2)
在比较其他应用程序上下文文件并重新阅读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"/>