使用Spring时使用MongoDB服务时出现异常

时间:2014-05-21 14:28:03

标签: spring mongodb spring-data

我创建了一个简单的项目'使用SpringData的mongoDB'来读写MongoDB。该项目按预期工作。现在,当我尝试将项目集成到另一个现有的独立Java项目时,我看到了一个例外。         为了进一步调试这个问题,我在独立的Java应用程序中添加了这两行代码

mongo = new MongoClient("xx.xx.xx.xx");
SimpleMongoDbFactory simpleMongoDbFactory = new SimpleMongoDbFactory(mongo, "MongoDBName");

到目前为止没有例外,但是当我添加以下行时,我得到一个例外。

    MongoTemplate mongoTemplate = new MongoTemplate(mongo, "MongoDBName" );

有谁能告诉我为什么我会看到这个错误?我可以在不使用SpringData的情况下使用它。

使用的罐子:

mongo-java-driver-2.11.1.jar, spring-data-mongodb-1.2.1.RELEASE.jar, spring-data-commons-1.5.1.RELEASE.jar, spring-tx-4.0.2.RELEASE.jar, cglib-2.2.2.jar

应用程序日志文件中的异常堆栈跟踪:

java.lang.NullPointerException
            at org.springframework.core.SerializableTypeWrapper$TypeProxyInvocationHandler.invoke(SerializableTypeWrapper.java:239)
            at org.springframework.core.$Proxy0.hashCode(Unknown Source)
            at org.springframework.util.ObjectUtils.nullSafeHashCode(ObjectUtils.java:273)
            at org.springframework.core.ResolvableType.hashCode(ResolvableType.java:779)
            at org.springframework.util.ConcurrentReferenceHashMap.getHash(ConcurrentReferenceHashMap.java:214)
            at org.springframework.util.ConcurrentReferenceHashMap.getReference(ConcurrentReferenceHashMap.java:246)
            at org.springframework.util.ConcurrentReferenceHashMap.get(ConcurrentReferenceHashMap.java:226)
            at org.springframework.core.ResolvableType.forType(ResolvableType.java:1154)
            at org.springframework.core.ResolvableType.forType(ResolvableType.java:1133)
            at org.springframework.core.ResolvableType.forTypes(ResolvableType.java:812)
            at org.springframework.core.ResolvableType.getInterfaces(ResolvableType.java:388)
            at org.springframework.core.ResolvableType.as(ResolvableType.java:350)
            at org.springframework.core.convert.support.GenericConversionService.getRequiredTypeInfo(GenericConversionService.java:266)
            at org.springframework.core.convert.support.GenericConversionService.addConverterFactory(GenericConversionService.java:103)
            at org.springframework.core.convert.support.DefaultConversionService.addScalarConverters(DefaultConversionService.java:79)
            at org.springframework.core.convert.support.DefaultConversionService.addDefaultConverters(DefaultConversionService.java:63)
            at org.springframework.core.convert.support.DefaultConversionService.<init>(DefaultConversionService.java:50)
            at org.springframework.core.convert.support.ConversionServiceFactory.createDefaultConversionService(ConversionServiceFactory.java:69)
            at org.springframework.data.mongodb.core.convert.MappingMongoConverter.<init>(MappingMongoConverter.java:100)
            at org.springframework.data.mongodb.core.MongoTemplate.getDefaultMongoConverter(MongoTemplate.java:1692)
            at org.springframework.data.mongodb.core.MongoTemplate.<init>(MongoTemplate.java:189)
            at org.springframework.data.mongodb.core.MongoTemplate.<init>(MongoTemplate.java:154)
            at com.rccl.promotions.processor.AbstractPromotionProcessor.process(AbstractPromotionProcessor.java:92)
            at com.rccl.promotions.processor.MessageProcessor.process(MessageProcessor.java:73)
            at com.rccl.promotions.messageadapter.processor.MessageProcessorTest.testGetVoyageBestRatePromotion_step1(MessageProcessorTest.java:66)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:76)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
            at java.lang.reflect.Method.invoke(Method.java:607)
            at junit.framework.TestCase.runTest(TestCase.java:168)
            at junit.framework.TestCase.runBare(TestCase.java:134)
            at junit.framework.TestResult$1.protect(TestResult.java:110)
            at junit.framework.TestResult.runProtected(TestResult.java:128)
            at junit.framework.TestResult.run(TestResult.java:113)
            at junit.framework.TestCase.run(TestCase.java:124)
            at junit.framework.TestSuite.runTest(TestSuite.java:232)
            at junit.framework.TestSuite.run(TestSuite.java:227)
            at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
            at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
            at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
            at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRun`enter code here`ner.java:467)
            at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
            at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
            at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

2 个答案:

答案 0 :(得分:0)

检查Spring应用程序上下文,为方便起见,您可以使用与mongo相关的命名空间支持:

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:mongo="http://www.springframework.org/schema/data/mongo"
   xsi:schemaLocation="http://www.springframework.org/schema/data/mongo
      http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <mongo:mongo id="mongo" host="127.0.0.1" port="27017" />
    <mongo:db-factory id="mongoDbFactory" mongo-ref="mongo" dbname="MongoDBName" />

    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
        <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
    </bean>

    <mongo:repositories base-package="path.to.your.repository.packages"/>

</beans>

您可以在GitHub上查看我已经设置过的示例。

答案 1 :(得分:0)

之所以发生这种情况是因为你的项目同时具有spring 3.0和spring 4.0+作为依赖项(因为这两个版本具有不同的工件名称,是的,两者可以共存!)。两个版本都有这个类'ObjectUtils.java',但它们的'nullSafeHashCode'方法有不同的实现!

以spring 2.x和spring 4.x为例: 在v2中,

public static int nullSafeHashCode(Object obj) {
    if (obj == null) {
        return 0;
    }
    if (obj.getClass().isArray()) {
        if (obj instanceof Object[]) {
            return nullSafeHashCode((Object[]) obj);
        }
        if (obj instanceof boolean[]) {
            return nullSafeHashCode((boolean[]) obj);
        }
        if (obj instanceof byte[]) {
            return nullSafeHashCode((byte[]) obj);
        }
        if (obj instanceof char[]) {
            return nullSafeHashCode((char[]) obj);
        }
        if (obj instanceof double[]) {
            return nullSafeHashCode((double[]) obj);
        }
        if (obj instanceof float[]) {
            return nullSafeHashCode((float[]) obj);
        }
        if (obj instanceof int[]) {
            return nullSafeHashCode((int[]) obj);
        }
        if (obj instanceof long[]) {
            return nullSafeHashCode((long[]) obj);
        }
        if (obj instanceof short[]) {
            return nullSafeHashCode((short[]) obj);
        }
    }
    return obj.hashCode(); // The problem comes from here
}

在v4中,

public static boolean nullSafeEquals(Object o1, Object o2) {
    if (o1 == o2) {
        return true;
    }
    if (o1 == null || o2 == null) {
        return false;
    }
    if (o1.equals(o2)) {
        return true;
    }
    if (o1.getClass().isArray() && o2.getClass().isArray()) {
        if (o1 instanceof Object[] && o2 instanceof Object[]) {
            return Arrays.equals((Object[]) o1, (Object[]) o2);
        }
        if (o1 instanceof boolean[] && o2 instanceof boolean[]) {
            return Arrays.equals((boolean[]) o1, (boolean[]) o2);
        }
        if (o1 instanceof byte[] && o2 instanceof byte[]) {
            return Arrays.equals((byte[]) o1, (byte[]) o2);
        }
        if (o1 instanceof char[] && o2 instanceof char[]) {
            return Arrays.equals((char[]) o1, (char[]) o2);
        }
        if (o1 instanceof double[] && o2 instanceof double[]) {
            return Arrays.equals((double[]) o1, (double[]) o2);
        }
        if (o1 instanceof float[] && o2 instanceof float[]) {
            return Arrays.equals((float[]) o1, (float[]) o2);
        }
        if (o1 instanceof int[] && o2 instanceof int[]) {
            return Arrays.equals((int[]) o1, (int[]) o2);
        }
        if (o1 instanceof long[] && o2 instanceof long[]) {
            return Arrays.equals((long[]) o1, (long[]) o2);
        }
        if (o1 instanceof short[] && o2 instanceof short[]) {
            return Arrays.equals((short[]) o1, (short[]) o2);
        }
    }
    return false;
}

在spring-data-mongodb中,它应该在类路径中使用v4,但实际上,JVM错误地加载了v2。使用mvn dependency:tree来检查所有底层依赖项。