【问题标题】:Spring profiles: Repository bean cannot be autowiredSpring 配置文件:无法自动装配存储库 bean
【发布时间】:2015-08-06 07:08:02
【问题描述】:

我在尝试使用我创建的一些 Spring 配置文件运行 JUnit 测试时遇到问题。实际上,配置文件确实可以正常工作,但无法自动装配的存储库 bean 除外。

这是我在 spring-integration-context.xml 中用于配置持久性的代码片段:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
   .... Some namespaces definitions

    <beans profile="dev">
        <jdbc:embedded-database id="dataSource" type="H2" />

        <!-- Define the JPA transaction manager -->
        <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
            <constructor-arg ref="entityManagerFactory" />
        </bean>


        <bean id="entityManagerFactory"
            class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="jpaVendorAdapter" ref="vendorAdaptor" />
            <property name="packagesToScan" value="guiatv.persistence.*" />
        </bean>

        <bean id="abstractVendorAdaptor" abstract="true">
            <property name="generateDdl" value="true" />
            <property name="database" value="H2" />
            <property name="showSql" value="false" />
        </bean>

        <bean id="entityManager"
            class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
            <property name="entityManagerFactory" ref="entityManagerFactory" />
        </bean>

        <bean id="vendorAdaptor"
            class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
            parent="abstractVendorAdaptor">
        </bean>
    </beans>

    ... Some other beans, some of them with the same "dev" profile

</beans>

所以我认为用“beans profile”元素包装整个持久性配置就足够了。

现在,来自我的 JUnit 测试类:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = ApplicationTest.class)
@ActiveProfiles("dev")

public class ScheduleLoaderTests {
    @Autowired
    ScheduleLoader schedLoader;

    @Autowired
    ScheduleRepository shedRep;

    @Test
    public void someTest() { ... }
}

然后我用以下代码注释我的 ApplicationTest 类:

@ImportResource("classpath:/META-INF/spring/integration/spring-integration-context.xml")

但是当我运行测试时,我得到了以下异常:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [guiatv.persistence.repository.ScheduleRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

请注意,在我的测试类中,ScheduleLoader 之前是自动装配的,实际上该 bean 也在开发配置文件中。如果我尝试在没有 @ActiveProfiles("dev") 注释,然后 ScheduleLoader failed autowiring 抛出异常,正如预期的那样(因为它只在 dev 配置文件中)。

那么 spring 配置文件和我的持久性配置有什么问题?

PD:我已确保我的存储库和实体类在其中

guiatv.persistence.*

包。

顺便说一句,我使用的是 Spring Boot,所以我也尝试通过在我的 application.properties 文件中添加 spring.profiles.active=dev 来使其工作,而不是编写 @ActiveProfiles("dev") 注释.

更新

此外,如果我尝试删除每个“beans profile”元素,以便每个组件都是根“beans”元素的子元素,那么无论没有 @ActiveProfiles("dev") 注释还是使用@ActiveProfiles("default") 注释。

【问题讨论】:

  • 配置存储库的部分在哪里(我猜,那些是 Spring Data JPA 的存储库)?

标签: spring hibernate jpa spring-boot spring-profiles


【解决方案1】:

现在它正在工作。

@dunni 你看到的是整个持久性相关的配置。是的,我使用 JPA,但正如我所说,我使用的是 Spring Boot。似乎 Spring Boot 为您配置了 persistence.xml (我猜这就是您要问的)。

所以,唯一缺少的是我的 ApplicationTest 类上的 @SpringBootApplication,以便让 Spring Boot 自动配置存储库。

抱歉,我的更新令人困惑,我也错过了 @SpringBootApplication

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-09
    • 2016-09-27
    • 2017-03-28
    • 2019-02-28
    • 1970-01-01
    • 1970-01-01
    • 2012-12-04
    • 2014-03-22
    相关资源
    最近更新 更多