我花了一些时间解决Spring Data中缺少org.joda.time.DateTime->java.util.Date
转换器的问题(当Joda-Time在类路径中时,默认情况下应该启用该转换器)。我找到了一个理由,但它在Spring中产生了一个关于@Configuration
注释的问题。我应该注释配置类作为@Configuration进行测试吗?
使用AbstractMongoConfiguration
从弹簧数据mongodb的标准应用程序配置:
@Configuration
@ComponentScan
@EnableMongoRepositories
public class AppConfig extends AbstractMongoConfiguration { ... }
其中显式使用的AppConfig类(斯波克,但是在内部,使用由弹簧测试提供的机制)的试验:
@ContextConfiguration(classes = AppConfig)
class JodaDocRepositorySpec extends Specification {
@Autowired
private JodaDocRepository jodaDocRepository
def "save document with DateTime"() {
given:
def jodaDoc = new JodaDoc(DateTime.now())
when:
def savedJodaDoc = jodaDocRepository.save(jodaDoc)
then:
savedJodaDoc.id
}
}
它工作正常。但是,当在AppConfig中@Configuration注解去除 /评论:
//@Configuration
@ComponentScan
@EnableMongoRepositories
public class AppConfig extends AbstractMongoConfiguration { ... }
测试失败:
org.springframework.core.convert.ConverterNotFoundException:
No converter found capable of converting from type org.joda.time.DateTime to type java.util.Date
AFAIK它不需要使用@Configuration
的配置类,当它是明确注册的在上下文中(通过@ContextConfiguration
中的类或AnnotationConfigWebApplicationContext
中的register()
方法)。无论如何处理这些类并找到所有已声明的bean。有时不使用@Configuration
可以防止在不同测试使用的测试环境中的相同包中有2个类似的配置类时,通过组件扫描进行检测。
因此,我认为它可能会在上下文中导致不同内部bean处理的错误,具体取决于使用情况而不是@Configuration
注释。我比较了这两种情况下的Spring日志,并且存在一些差异,但我无法确定它们在Spring内部类中造成了什么。在提交错误之前,我想问问:
我的问题。有一个可解释的原因,为什么相同配置类的Spring(针对@ContextConfiguration
明确指出)使用(或不)转换器用于Joda-Time,具体取决于@Configuration
注释的存在吗?
我也创建了一个quickstart project重现这个问题。 spring-data-mongodb 1.3.3,spring 4.0.0,joda-time 2.3。