我正在编写一个自定义对象与DBObject之间的转换器, 从MongoDB写入的作者工作正常,但从DB检索时,从MongoDB读取的读者没有得到呼叫
问题是我该怎么做才能使spring / mongodb使用它
注意:因为我有一个构造函数,所以我看到它确实在创建时就到达了
附加了转换器和配置的全部代码
配置:
@配置
@EnableMongoRepositories(basePackages =“ .....”)
公共类MongoConfig扩展了AbstractMongoConfiguration {
private String databaseName;
private String mongoHost;
private int mongoPort;
@Autowired
private Util util;
private final List<Converter<?,?>> converters = new arrayList<Converter<?, ?>>();
@Override
protected String getDatabaseName() {
return databaseName;
}
@Bean
@Override
public MongoCustomConversions customConversions() {
converters.add(new CustomObjectWriteConverter (util));
converters.add(new CustomObjectReadConverter(util));
return new MongoCustomConversions(converters);
}
@Override
public MongoClient mongoClient() {
return new MongoClient(mongoHost, mongoPort);
}
}
作家:
@Component
@WritingConverter
公共类CustomObjectWriteConverter实现了Converter {
private Util util;
public CustomObjectWriteConverter( Util util;) {
this.util= util;
}
@Override
public DBObject convert(CustomObject source) {
//
// Some code
//
return dbo;
}
}
阅读器:
@Component
@ReadingConverter
公共类CustomObjectReadConverter实现了Converter {
private Util util;
public CustomObjectReadConverter(Util util ) {
this.util = util ;
}
@Override
public CustomObject convert(DBObject source) {
//
// Some code
//
return customObject ;
}
}