gpt4 book ai didi

java - 使用 @SpringApplicationConfiguration 在多模块项目中运行域的测试会抛出 NoSuchBeanDefinitionException?

转载 作者:行者123 更新时间:2023-12-02 04:39:21 26 4
gpt4 key购买 nike

我的多模块项目结构如下:

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 类吗?在本例中,这是在服务模块内部,因此域不知道此类。您能解释一下 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 自动注入(inject)数据源。 (这个注解一般继承自@SpringBootApplication)

您有多种选择:

  • @SpringApplicationConfiguration 引用使用 @SpringBootApplication 注解的应用程序类

  • @EnableAutoConfiguration添加到DAOConfiguration

  • ...(你明白这一点)

关于java - 使用 @SpringApplicationConfiguration 在多模块项目中运行域的测试会抛出 NoSuchBeanDefinitionException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30359055/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com