No qualifying bean of type [javax.sql.DataSource] found
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.
<!-- 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"/>
rray
Over fifteen years experience working from various leadership roles within the full Software Development Life Cycle (SDLC) covering a market or government based sector. Superior soft skills honed and fine-tuned through extensive practice while in the U.S. military: a strong commitment and sense of responsibility, goal oriented, coach and team player.
Updated on June 13, 2022Comments
-
rray almost 2 years
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:
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: {}
This is my testing application context file:
<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 CompanyDAOTest Class.
@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