【发布时间】:2015-05-20 19:46:40
【问题描述】:
我的多模块项目结构如下:
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
最后是我的 DAO 配置
@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 模块中,因此域不知道这个类。你能解释一下 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);
}
}
【问题讨论】:
-
你的 DataSource bean 是在哪里定义的?为什么不在 DAOConfiguration 类中定义它,因为这是需要它的地方?
-
因为 Spring Boot 构建了它。这就是我运行服务器时的方式,spring boot 会生成在 persistence.properties 文件中指定的 bean 读取属性(在这种情况下)。所以显然我没有用 Spring boot 运行测试,这就是问题所在......
-
见docs.spring.io/spring-boot/docs/current/reference/html/… 我想它应该可以解决问题。
-
我认为问题来自初始化程序,而不是来自我正在加载的类......我认为通过某种方式我应该通过位于服务模块中的 @SpringBootApplication 注释的应用程序(但是我不能这样做,因为域不知道服务)。你怎么看?
-
可以分享一下服务层的配置类吗?
标签: java spring spring-boot spring-data-jpa spring-test