我的多模块项目结构如下:
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);
}
}
答案 0 :(得分:2)
我解决了同样的问题。
问题来自@SpringApplicationConfiguration(classes = DAOConfiguration.class)
。
您必须引用一个用@EnableAutoConfiguration
注释的类,以便Spring Boot自动注入数据源。 (此注释通常继承自@SpringBootApplication
)
您有几种选择:
使用@SpringBootApplication
@SpringApplicationConfiguration
注释您的应用程序类
将@EnableAutoConfiguration
添加到DAOConfiguration
类
...(您理解这一点)