gpt4 book ai didi

java - 找不到 [javax.sql.DataSource] 类型的合格 bean

转载 作者:行者123 更新时间:2023-11-30 08:49:37 24 4
gpt4 key购买 nike

我正在努力理解为什么会发生此错误。我正在将教程移植到最新版本的 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: {}

这是我的测试应用程序上下文文件:

<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");
}

如有任何建议,我们将不胜感激。

俄罗斯人

最佳答案

在比较其他应用程序上下文文件并重新阅读 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"/>

关于java - 找不到 [javax.sql.DataSource] 类型的合格 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31347959/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com