我在 Play 2.4.2(最新版本)框架应用程序中使用 ModelMapper 0.7.4(最新版本)。Play 2.4 内置了一个内部 Google Guice 依赖注入解决方案,我们的应用程序是从 Guice 到 Spring Framework 依赖注入解决方案的手动桥接,以使 Play 2.4 与 Spring 一起工作。所以通信从 Play 到 Guice 再到 Spring。
事情(使用 Spring 的依赖注入)似乎工作正常,但是当在测试开发环境中更改随机 Java 类时,Play 会自动重新加载该类或 webapp。这种重新加载通常可以正常工作,但是当 ModelMapper 在此 Play 设置中用作 Spring Bean 时,它似乎会导致 ModelMapper 出现问题。(但是通过在设置中手动创建一个 Spring 容器然后将 ModelMapper 作为 Spring bean 联系来绕过 Guice-Spring 桥时无法重现该问题。)
错误是:
Caused by: org.modelmapper.ConfigurationException: ModelMapper configuration errors:
1) Failed to configure mappings
1 error
at org.modelmapper.internal.Errors.throwConfigurationExceptionIfErrorsExist(Errors.java:241) ~[modelmapper-0.7.4.jar:na]
at org.modelmapper.internal.ExplicitMappingBuilder.build(ExplicitMappingBuilder.java:207) ~[modelmapper-0.7.4.jar:na]
at org.modelmapper.internal.TypeMapImpl.addMappings(TypeMapImpl.java:72) ~[modelmapper-0.7.4.jar:na]
at org.modelmapper.internal.TypeMapStore.getOrCreate(TypeMapStore.java:101) ~[modelmapper-0.7.4.jar:na]
at org.modelmapper.ModelMapper.addMappings(ModelMapper.java:93) ~[modelmapper-0.7.4.jar:na]
at configs.AppConfig.modelMapper(AppConfig.java:109) ~[na:na]
at configs.AppConfig$$EnhancerBySpringCGLIB$$b19a8688.CGLIB$modelMapper$2(<generated>) ~[na:na]
at configs.AppConfig$$EnhancerBySpringCGLIB$$b19a8688$$FastClassBySpringCGLIB$$1f1c1728.invoke(<generated>) ~[na:na]
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) ~[spring-core-4.2.0.RELEASE.jar:4.2.0.RELEASE]
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:318) ~[spring-context-4.2.0.RELEASE.jar:4.2.0.RELEASE]
at configs.AppConfig$$EnhancerBySpringCGLIB$$b19a8688.modelMapper(<generated>) ~[na:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_45]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_45]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_45]
at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_45]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162) ~[spring-beans-4.2.0.RELEASE.jar:4.2.0.RELEASE]
... 64 common frames omitted
Caused by: java.lang.ClassCastException: project.entities.User$$EnhancerByModelMapper$$f1b8f0f9 cannot be cast to project.entities.User
at configs.AppConfig$1.configure(AppConfig.java:106) ~[na:na]
at org.modelmapper.PropertyMap.configure(PropertyMap.java:383) ~[modelmapper-0.7.4.jar:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_45]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_45]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_45]
at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_45]
at org.modelmapper.internal.ExplicitMappingBuilder.build(ExplicitMappingBuilder.java:195) ~[modelmapper-0.7.4.jar:na]
... 78 common frames omitted
这仅在类重新加载时发生,而不是在没有重新加载完成时发生。此外,未使用 modelMapper.addMappings(aPropertyMap) 时也不会出现此问题。Spring AppConfig 类如下所示:
@Configuration
public class AppConfig {
@Bean
public ModelMapper modelMapper() {
ModelMapper modelMapper = new ModelMapper();
// BEGIN: WITHOUT THE FOLLWOING CODE, it works fine
PropertyMap<CreateUserFormDTO, User> userMap = new PropertyMap<CreateUserFormDTO, User>() {
@Override
public void configure() {
map().setPassword(source.getPassword1());
}
};
modelMapper.addMappings(userMap);
// END
return modelMapper;
}
}
使用普通的 Spring @Autowire 注入访问 ModelMapper。User 和 CreateUserFormDTO 类只是 POJO。
问题可能是什么?