MongoDB整合Spring4.2.1的版本

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/lhh143400/article/details/100720149

参考链接:https://blog.csdn.net/ljqwstc/article/details/78399261

每个人遇到的问题可能会不一样,参考链接没有解决我的问题,但间接使我解决了问题。以下是我个人的配置信息

1.我使用的包:

spring-context-4.2.1.RELEASE.jar

bson-2.14.3.jar                                               (用太高,我这会报错)

mongo-java-driver-2.14.3 .jar

spring-data-mongodb-1.7.2.RELEASE.jar      (用太高,我这会报错)

2.xml 配置

<?xml version="1.0" encoding="UTF-8"?>
<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" replica-set="${mongo.hostport}">

        <mongo:options connections-per-host="8"
            threads-allowed-to-block-for-connection-multiplier="4"
            connect-timeout="1000" max-wait-time="1500" auto-connect-retry="true"
            socket-keep-alive="true" socket-timeout="1500" slave-ok="true"
            write-number="1" write-timeout="0" write-fsync="true" />
    </mongo:mongo>


    <mongo:db-factory username="${mongo.username}"
        password="${mongo.password}" dbname="${mongo.dbname}" mongo-ref="mongo" />

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

    <!-- 注入 gridFsTemplate    bean -->
    <bean id="gridFsTemplate" class="org.springframework.data.mongodb.gridfs.GridFsTemplate">
        <constructor-arg ref="mongoDbFactory" />
        <constructor-arg ref="converter" />
    </bean>

</beans>

3.properties  配置

mongo.hostport=127.0.0.1:27017
mongo.dbname=testdb
mongo.username=
mongo.password=

4.gridFsTemplate工具类

想用MongoTemplate ,gridFsTemplate 都可以,这边讲解 gridFsTemplate 使用

    @Autowired
    private GridFsTemplate gridFsTemplate;

    @Override
    public String save(InputStream inputStream, String fileName) {
        String fileId = null;
        try {
            String file = gridFsTemplate.store(inputStream, fileName).toString();
            Map obj=JSON.parseObject(file,Map.class);
            Map oid=JSON.parseObject(obj.get("_id").toString(),Map.class);
            fileId=String.valueOf( oid.get("$oid"));

        } catch (Exception e) {
              e.printStackTrace();
        }
        return fileId;
    }

 注意标红:这是包不一样 ,所以返回参数解析有所不同

遇到的问题

问题1:九月 11, 2019 12:01:39 上午 org.apache.catalina.core.StandardContext listenerStart
严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongo': Cannot create inner bean '(inner bean)#470e90e0' of type [org.springframework.data.mongodb.core.MongoOptionsFactoryBean] while setting bean property 'mongoOptions'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#470e90e0': Instantiation of bean failed; nested exception is java.lang.IllegalAccessError: tried to access method org.bson.types.ObjectId.<init>(III)V from class com.mongodb.Bytes
    
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#470e90e0': Instantiation of bean failed; nested exception is java.lang.IllegalAccessError: tried to access method org.bson.types.ObjectId.<init>(III)V from class com.mongodb.Bytes
    
Caused by: java.lang.IllegalAccessError: tried to access method org.bson.types.ObjectId.<init>(III)V from class com.mongodb.Bytes
解决: 

原本 是 
org.mongodb--bson--3.4.3版本

改为 org.mongodb--bson--2.14.3 版本

问题2:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'MongoTemplate' defined in class path resource [xml/mongodb.xml]: Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.mongodb.core.MongoTemplate]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/data/domain/ExampleMatcher$StringMatcher

解决方案
原本jar是:  spring-data-mongodb-1.10.7.RELEASE.jar

改为:spring-data-mongodb-1.7.2.RELEASE.jar 

猜你喜欢

转载自blog.csdn.net/lhh143400/article/details/100720149