简体   繁体   English

Spring Boot与MongoDB集成

[英]Spring Boot integration with MongoDB

I am trying to integrate monodb with my springBoot application 1st time, But I am stuck with the error My code is below 我正在尝试第一次将monodb与我的springBoot应用程序集成,但是我陷入了错误,我的代码如下

package com.myProject.customerview;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import org.springframework.scheduling.annotation.EnableScheduling;

@EnableScheduling
@ImportResource("classpath*:spring/application-context.xml")
@ComponentScan({ "com.myproject.payments", "com.myproject.customerview" })
// @EnableJpaRepositories("com.myproject.customerview.repository")
@EnableMongoRepositories
@SpringBootApplication
public class CustomerViewApplication {

    public static void main(String args[]) {
        SpringApplication application = new SpringApplication(
                CustomerViewApplication.class);
        application.run(args);
    }

}

and Controller 和控制器

package com.myproject.customerview.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.myproject.customerview.constants.RestURIConstants;
import com.myproject.customerview.response.ServiceResponse;
import com.myproject.customerview.service.CustomerViewService;

@RestController
@RequestMapping(RestURIConstants.CUSTOMER_VIEW)
public class CustomerViewController {

    @Autowired
    private CustomerViewService customerViewService ;
       @RequestMapping(value = RestURIConstants.USER_ID, method = RequestMethod.GET)
       public ServiceResponse<String> getMirrorAccountReport(
                @PathVariable("userId") String userId, HttpServletRequest servletRequest)
                throws Exception {
          ServiceResponse<String> serviceResponse = new ServiceResponse<String>();
          String response = "success:" ;
                  customerViewService.saveCustomerViewEntity();
          serviceResponse.setResponse(response);
          return serviceResponse;
       }
}

service 服务

package com.myproject.customerview.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.myproject.customerview.entity.CustomerViewEntity;
import com.mmyproject.customerview.repository.CustomerViewRepository;
import com.myproject.customerview.service.CustomerViewService;

@Service
public class CustomerviewServiceImpl implements CustomerViewService {

    @Autowired
    private CustomerViewRepository customerRepository ;
    @Override
    public void saveCustomerViewEntity() {
        CustomerViewEntity entity = new CustomerViewEntity() ;
        //setting data in enity....
        customerRepository.saveCustomerView(entity);
    }
}

dao interface layer dao接口层

package com.myproject.customerview.dao;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.springframework.data.domain.Sort;

import com.myproject.customerview.entity.CustomerViewEntity;

public interface CustomerViewMongoDao {
    public void saveCustomerView(CustomerViewEntity entity) ;
}

its impl 它的影响

package com.myproject.customerview.dao.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Repository;

import com.myproject.customerview.constants.CustomerViewConstants;
import com.myproject.customerview.dao.CustomerViewMongoDao;
import com.myproject.customerview.entity.CustomerViewEntity;

@Repository
public class CustomerViewMongoDaoImpl implements CustomerViewMongoDao{

    @Autowired
    MongoTemplate mongoTemplate ;   
    @Override
    public void saveCustomerView(CustomerViewEntity entity) {
        mongoTemplate.insert(entity, CustomerViewConstants.CUSTOMER_VIEW_COLLECTION);
    }
}

its repository 它的仓库

package com.myproject.customerview.repository;

import org.springframework.data.mongodb.repository.MongoRepository;

import com.myproject.customerview.dao.CustomerViewMongoDao;
import com.myproject.customerview.entity.CustomerViewEntity;

public interface CustomerViewRepository extends MongoRepository<CustomerViewEntity,String>, CustomerViewMongoDao {


}

and application-context.xml 和application-context.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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mybatis="http://mybatis.org/schema/mybatis-spring" xmlns:task="http://www.springframework.org/schema/task"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mongo="http://www.springframework.org/schema/data/mongo"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.5.xsd">

    <mvc:annotation-driven />
    <mvc:resources mapping="/resources/**" location="classpath:/resources" />

    <bean id="imsPlaceholderConfig" class="com.myproject.customerview.utils.SpringPropertiesUtil">
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
        <property name="locations">
            <list>
                <value>file:${config.path}/app.properties</value>
                <value>file:${config.path}/api.properties</value>
                <value>file:${config.path}/db.properties</value>
                <value>classpath*:*application.properties</value>
            </list>
        </property>
    </bean>

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".html" />
    </bean>

    <!-- <mongo:mongo replica-set="127.0.0.1:27017,127.0.0.1:27017,127.0.0.1:27017" id = "mongo" />

    <mongo:db-factory dbname="customer_view" mongo-ref="mongo" id="mongoDbFactory"/>

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

    <mongo:repositories base-package="com.myproject.customerview.repository" />
     -->


    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName"
            value="${payment.customerview.database.driverClassName}" />
        <property name="url" value="${payment.customerview.database.url}" />
        <property name="username" value="${payment.customerview.database.username}" />
        <property name="password" value="${payment.customerview.database.password}" />
        <property name="maxActive" value="${payment.customerview.database.maxactive}" />
        <property name="maxWait" value="${payment.customerview.database.maxwait}" />
        <property name="maxIdle" value="${payment.customerview.database.maxIdle}" />
        <property name="initialSize" value="${payment.customerview.database.inititalsize}" />
        <property name="minIdle" value="${payment.customerview.database.minIdle}" />
        <property name="timeBetweenEvictionRunsMillis"
            value="${payment.customerview.database.timebetweenevictionrunsmillis}" />
        <property name="minEvictableIdleTimeMillis"
            value="${payment.customerview.database.minevictableidletimemillis}" />
    </bean>


    <!-- MyBatis related beans -->
    <bean id="sqlSessionFactoryMybatis" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:myBatis/sqlmap-config.xml" />
    </bean>

    <bean name="myBatisIMSDB" id="myBatisIMSDB"
        class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactoryMybatis" />
        <constructor-arg index="1" value="REUSE" />
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" />
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage"
            value="com.myproject.customerview.dao.mapper" />
        <property name="sqlSessionTemplateBeanName" value="myBatisIMSDB"></property>
    </bean>
</beans>

I am getting following error. 我收到以下错误。

java.lang.IllegalStateException: Failed to introspect annotations: class org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfigureRegistrar$EnableMongoRepositoriesConfiguration

I have wrongly added this dependency in my pom.xml 我在pom.xml中错误地添加了此依赖项

     <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-mongodb</artifactId>
        <version>1.8.0.RELEASE</version>
     </dependency>

instead of this when i used 而不是当我用

     <dependency>
        <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-data-mongodb</artifactId>
    </dependency>

my projects starts working fine. 我的项目开始正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM