Spring Data Couchbase和Spring Data MongoDB同时使用

时间:2015-06-19 17:34:51

标签: spring spring-data couchbase

我想同时使用Spring Data Couchbase和Spring Data MongoDB。 但是它发生了错误,它们都定义了hava bean customConversions 。 那么,怎么处理呢?

这是stacktrace

Caused by: java.lang.ClassCastException: org.springframework.data.mongodb.core.convert.CustomConversions cannot be cast to org.springframework.data.couchbase.core.convert.CustomConversions
    at com.*.*.config.dev.DevelopmentCouchbaseConfig$$EnhancerBySpringCGLIB$$64a18d5.customConversions(<generated>)
    at org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration.couchbaseMappingContext(AbstractCouchbaseConfiguration.java:157)
    at com.*.*.config.dev.DevelopmentCouchbaseConfig$$EnhancerBySpringCGLIB$$64a18d5.CGLIB$couchbaseMappingContext$13(<generated>)
    at com.*.*.config.dev.DevelopmentCouchbaseConfig$$EnhancerBySpringCGLIB$$64a18d5$$FastClassBySpringCGLIB$$7675b050.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:309)
    at com.*.*.config.dev.DevelopmentCouchbaseConfig$$EnhancerBySpringCGLIB$$64a18d5.couchbaseMappingContext(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
    ... 65 more

1 个答案:

答案 0 :(得分:2)

我找到了方法:

只需覆盖并重命名bean。

这是Couchbase配置:

public class CouchbaseConfig extends AbstractCouchbaseConfiguration {
    @Override
    @Bean(name = "couchbaseConversions")
    public CustomConversions customConversions() {
        return new CustomConversions(Collections.emptyList());
    }
}

这是MongoDB配置:

public class MongoDBConfig extends AbstractMongoConfiguration {
    @Override
    @Bean(name = "mongoDBConversions")
    public CustomConversions customConversions() {
        return new CustomConversions(Collections.emptyList());
    }
}

感谢。