简体   繁体   中英

Unable to autowire spring repository beans

I continue getting the following error:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No qualifying bean of type 'StorageItemRepository'

My beans.xml is set to find all - located in META-INF/beans.xml of every module resource file. My Repository class does have @Repository on it and my web.xml is definitely configured to find the bean.

It's worth noting - this working when deployed - just not for the below unit test.

My unit test:

 /**
 * Test for {@link StorageItemRepository}
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:JDBCConfig.xml"})
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = {"com.app.storage.persistence.repository"})
public class StorageItemRepositoryTest {

/** {@link StorageItemRepository} */
@Autowired
private StorageItemRepository storageItemRepository;

/**
 * Finds all storage items in db.
 */
@Test
public void checkFindAllItems(){

    final StorageItemPersistenceModel storageItemPersistenceModel = new StorageItemPersistenceModel();
    storageItemPersistenceModel.setId(1L);
    storageItemPersistenceModel.setDateStored(new DateTime());
    storageItemPersistenceModel.setName("Name");
    storageItemRepository.save(storageItemPersistenceModel);
}
}

JDBCConfig file:

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/storage_app_schema" />
    <property name="username" value="root" />
    <property name="password" value="dollar123" />
</bean>

<bean id="myEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="packagesToScan" value="com.app.storage.persistence.model" />
    <property name="dataSource" ref="dataSource" />
    <property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
    <property name="persistenceUnitName" value="app_test"/>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>
</bean>

<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    <property name="showSql" value="true"/>
    <property name="generateDdl" value="true"/>
    <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect"/>
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="myEmf"/>
</bean>

</beans>

The persistence.xml is definitely fine and working so not the cause.

How are you running the unit test?

Since it is working when deployed, and not in the unit test, I am inclined to suspect that the unit test execution has a missing classpath, such as JDBCConfig.xml doesn't exist in the execution classpath

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM