繁体   English   中英

Spring 无法从 app-config-text.xml 中找到 app-config.xml

[英]Spring cannot find app-config.xml from app-config-text.xml

我正在为我的小应用程序的 DAO 层开发集成测试,但遇到了一个问题。 我使用 2 个不同的 application-config.xml 进行测试(使用 HSQLDB)和生产(MySql)。 即使我在测试中导入了我的生产配置文件,Spring 仍然找不到它。 结果,没有找到 [DAO] 类型的合格 bean 用于依赖(希望是由于这个问题)。 既没有显示完整路径,也没有为 xml 文件使用 **/ 或 / 帮助。

我收到的例外:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.phoneBook.DAO.ContactDAO] found for dependency

我的测试班:

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = {"classpath:**/application-context-test.xml"/*, "/application-config.xml"*/})
    @TransactionConfiguration(transactionManager = "transactionManager",defaultRollback = true)
    @Transactional
    public class ContactTest {
    //various fields for tests  
@Inject
        private ContactDAO contactIntegrDAO;
        @Inject
        private UserDAO userItegrDAO;

// various test methods
}

如果我在@ContextConfiguration 中取消注释“/application-config.xml”,我的测试配置(使用 HSQLDB 数据源)根本不会启动,并且我在生产数据库上调用了所有集成测试。

application-context-test.xml 中的测试配置:

<import resource="classpath:**/application-config.xml"/>

application-config.xml 中的生产配置:

<context:component-scan base-package="com.phoneBook" />

<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:properties/database.properties" />
</bean>
<!-- Alternative way to set database.properties -->
<!-- <context:property-placeholder location="classpath:properties\database.properties" 
    /> -->

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
</bean>

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

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="com.phoneBook.entities" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
    </property>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> 
            <prop key="hibernate.flushMode">${hibernate.flushMode}</prop>
            <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
        </props>
    </property>
</bean>

<bean id="persistenceExceptionTranslationPostProcessor"
    class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

项目结构:

在此处输入图片说明

任何帮助将不胜感激。

更新:

感谢asg 的回应,我更正了类路径的拼写错误,但不幸的是,问题仍然存在

问题可能与您的测试应用程序上下文有关。

application-context-test.xml 中的测试配置:

<import resource="clathpath:**/application-config.xml"/>

尝试将其更改为:

编辑:

<import resource="classpath:application-config.xml"/>

您正在将 'src/resources/application-config.xml' xml 文件导入到 'test/resources/application-config-test.xml' 文件中。

因此,将您的导入语句更改为上面编辑过的语句。 它应该适合你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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