SpringApplicationConfiguration

SpringApplicationConfiguration

我有这样的多模块项目结构:

project-example (parent)
-- project-example-domain: contains DAOs and Services
-- project-example-service: contains Controllers


服务模块将域作为依赖项,因此服务知道域,但不是。

因此,为了测试ClientDAO完整性(在这种情况下),我进行了以下测试:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = DAOConfiguration.class)
public class ClientDAOTests {

    @Autowired
    private ClientDAO dao;

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

}


当我运行它时,出现以下异常:

Caused by: 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: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1301)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
    ... 51 more


最后是我的DAOConfiguration

@Configuration
@PropertySource("classpath:persistence.properties")
@EnableJpaRepositories(basePackages = {"com.example.movies.domain.feature"})
@EnableTransactionManagement
public class DAOConfiguration {

    @Autowired
    private Environment env;

    @Autowired
    private DataSource dataSource;

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder) {
        LocalContainerEntityManagerFactoryBean entityManager = new LocalContainerEntityManagerFactoryBean();
        entityManager.setDataSource(this.dataSource);
        entityManager.setPackagesToScan(new String[] {"com.example.movies.domain"});
        JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        entityManager.setJpaVendorAdapter(vendorAdapter);
        return entityManager;
    }

    @Bean
    public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(emf);
        return transactionManager;
    }

    @Bean
    public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
        return new PersistenceExceptionTranslationPostProcessor();
    }

}


当我从该服务运行服务器时,它工作正常,Spring制作了DataSource bean。但是当我在域中运行测试时,我得到了前面提到的异常。

所以我认为我的问题是我将错误的参数传递给@SpringApplicationConfiguration。我不明白我必须通过什么...我应该通过@SpringBootApplication注释将它通过Application类吗?在这种情况下,它位于Service模块内部,因此Domain不知道此类。您能解释一下SpringApplicationConfiguration应该作为参数使我的测试失败吗?提前致谢!



编辑

这是我位于服务层的ApplicationRunner类:

@SpringBootApplication
@Import({ServicesConfiguration.class, CustomDataBindingConfiguration.class, CustomMappingConfiguration.class})
@PropertySource(value = {"application.properties"})
public class ApplicationRunner {

    public static void main(String[] args) {
        SpringApplication.run(ApplicationRunner.class, args);
    }

}

最佳答案

我解决了同样的问题。

问题来自@SpringApplicationConfiguration(classes = DAOConfiguration.class)
您必须引用一个用@EnableAutoConfiguration注释的类,以使数据源由Spring Boot自动注入。 (此注释通常是从@SpringBootApplication继承的)

您有几种选择:


@SpringBootApplication引用用@SpringApplicationConfiguration注释的应用程序类
@EnableAutoConfiguration添加到DAOConfiguration
...(您明白这一点)

09-27 06:57