Spring自动装配Bean给出类强制转换异常

时间:2013-07-29 14:53:23

标签: java spring

这是我的班级

@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);
    }
}

我收到以下错误

  

使用名称' mongoDB'创建bean时出错在类路径资源[com / fctest / mongo / config / MongoDBConfig.class]中定义: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无法强制转换为com.mongodb.MongoClient

0 个答案:

没有答案