运行 使用@SpringApplicationConfiguration 的多模块项目中域的测试抛出NoSuchBeanDefinitionException?
Running domain's tests in multi-module project with @SpringApplicationConfiguration throws NoSuchBeanDefinitionException?
我的多模块项目结构如下:
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 class 传递给它吗?在这种情况下,这是在服务模块内部,所以域不知道这个 class。您能解释一下 SpringApplicationConfiguration 应该将什么作为参数使我的测试失败吗?提前致谢!
编辑
这是我的 ApplicationRunner class 位于服务层:
@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
注释的 class,以便 Spring Boot 自动注入数据源。 (这个注解一般继承自@SpringBootApplication
)
您有多种选择:
引用您的应用程序 class,并在 @SpringApplicationConfiguration
中用 @SpringBootApplication
注释
将@EnableAutoConfiguration
添加到DAOConfiguration
class
...(你懂的)
我的多模块项目结构如下:
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 class 传递给它吗?在这种情况下,这是在服务模块内部,所以域不知道这个 class。您能解释一下 SpringApplicationConfiguration 应该将什么作为参数使我的测试失败吗?提前致谢!
编辑
这是我的 ApplicationRunner class 位于服务层:
@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
注释的 class,以便 Spring Boot 自动注入数据源。 (这个注解一般继承自@SpringBootApplication
)
您有多种选择:
引用您的应用程序 class,并在
@SpringApplicationConfiguration
中用 将
@EnableAutoConfiguration
添加到DAOConfiguration
class...(你懂的)
@SpringBootApplication
注释