Spring配置文件:无法自动装配存储库bean

时间:2015-05-24 19:36:03

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

尝试使用我创建的一些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( “类路径:/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也在dev配置文件中。如果我尝试不运行测试 @ActiveProfiles(“dev”)注释,然后按预期由 ScheduleLoader失败自动装配引发异常(因为它仅在dev配置文件中)。

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

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

之内
  

guiatv.persistence。*

包。

顺便说一下,我正在使用Spring Boot,所以我也尝试通过在application.properties文件中添加 spring.profiles.active = dev 来代替写作 @ActiveProfiles(“dev”)注释

更新

另外,如果我尝试删除每个“beans profile”元素,以便每个组件都是根“beans”元素的子元素,那么一切都运行正常,没有@ActiveProfiles(“dev”)注释或者@ActiveProfiles(“默认”)注释。

1 个答案:

答案 0 :(得分:0)

现在它正在运作。

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

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

对于我的混乱更新感到抱歉,我错过了 @SpringBootApplication

相关问题