0

这是我的课

@Configuration
class MongoDBConfig {
    private Logger logger = LoggingFactory.getLogger(getClass());
    @Bean
    @Scope(value = "singleton", proxyMode = ScopedProxyMode.INTERFACES)
    public MongoClient mongoClient() throws UnknownHostException {
        logger.error("In Dev mode:");
        return new MongoClient("fctest");

    }

    @Bean
    public DB mongoDB() throws UnknownHostException{
        try {
            return mongoClient().getDB(FCProperties.MONGO_DB_NAME);
        }catch (UnknownHostException ue){
            logger.error("Error occurred: "+ue.getMessage());
            throw ue;
        }
    }
}

这就是我使用它的方式 -

public class UserRepository extends BaseRepository {
    @Autowired
    public UserRepository(DB mongoDB) {
        super("users", mongoDB);
    }
}

我收到以下错误

在类路径资源 [com/fctest/mongo/config/MongoDBConfig.class] 中定义名称为“mongoDB”的 bean 创建错误:bean 实例化失败;嵌套异常是 org.springframework.beans.factory.BeanDefinitionStoreException: 工厂方法 [public com.mongodb.DB com.fctest.mongo.config.MongoDBConfig.mongoDB() throws java.net.UnknownHostException] 抛出异常;嵌套异常是 java.lang.ClassCastException: com.sun.proxy.$Proxy13 cannot be cast to com.mongodb.MongoClient

4

0 回答 0