感謝您的回覆。我想我找到了爲什麼_class被忽略的原因。你是對的,我使用了TypeMapper的不尋常組合。
讓我告訴你我的CustomTypeMapper:
public class CustomTypeMapper extends DefaultMongoTypeMapper {
public CustomTypeMapper (MongoMappingContext mappingContext) {
super(DEFAULT_TYPE_KEY,
Arrays.asList(new MappingContextTypeInformationMapper(
mappingContext)));
}
@Override
public <T> TypeInformation<? extends T> readType(DBObject source,
TypeInformation<T> basicType) {
// do some custom recognition
// or just to the common type mapping
return super.readType(source, basicType);
}
}
除了常見的類型識別特殊的一點是,其中MappingContextTypeInformationMapper用於使用@TypeAlias註釋構造函數。
這裏的關鍵是需要的MongoMappongContext。在非功能性的情況下,我初始化CustomTypeMapper像
<bean id="customTypeMapper" class="de.flexguse.repository.CustomTypeMapper">
<constructor-arg name="mappingContext">
<bean class="org.springframework.data.mongodb.core.mapping.MongoMappingContext" />
</constructor-arg>
</bean>
這工作,但錯了,因爲MongoMappingContext的新實例並沒有包含在
<mongo:mapping-converter id="customMappingConverter" db-factory-ref="mongoDbFactory"
type-mapper-ref="customTypeMapper" base-package="de.flexguse.model" >
<mongo:custom-converters>
... some custom converters ...
</mongo:custom-converters>
</mongo:mapping-converter>
通過設置基礎包屬性提供的任何TypeInformation
不幸的是,我無法弄清楚MongoMappingContext的創建地點是否可以用作構造函數參數,所以我發明了一個簡單的MongoMappingContextProvider,它使用@Autowire獲取MongoMappingContext
public class MongoMappingContextProvider {
@Autowired
private MongoMappingContext mappingContext;
/**
* @return the mappingContext
*/
public MongoMappingContext getMappingContext() {
return mappingContext;
}
}
的CustomTypeMapper的Spring的配置現在看起來是這樣
<bean id="mongoMappingContextProvider" class="de.flexguse.repository.MongoMappingContextProvider" />
<bean id="customTypeMapper" class="de.flexguse.repository.CustomTypeMapper">
<constructor-arg name="mappingContext" value="#{mongoMappingContextProvider.mappingContext}" />
</bean>
該解決方案爲我工作。
順便說一句,就一定要對我用我加@Document到這是堅持每一個模型都豆模型bean所有TypeInformations - 以防萬一;)
也許這是別人有幫助的類似的問題。
問候, 克里斯托夫
這裏的問題類似於http://stackoverflow.com/questions/22538256/java-isassignablefrom-not-working-post-hot-deploy。你可以看一下嗎? – Jaga