MongoConfig配置文件存在错误

难难难,道德玄,
不对知音不可谈,
对了知音谈几句,
不对知音枉废舌尖!

我用的是mongdb的数据库,而且是集群配置的,在启动项目会时不时的报出以下错误,让在配置文件中添加spring.main.allow-bean-definition-overriding=true 的配置,但是我在用时加上以上配置还是报错,然后我就修改MongConfig配置文件的名字,有时候就能用了。

今天我改了好多次名字,项目还是启动不了,换了个服务器还是报错,以前都是凑合着解决这个问题,但是一直没有找到原因,今天终于凑合不了了,望各位大佬们给提点一下子,万分感谢!

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.4.RELEASE)

09:49:23.730 [main] INFO  org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration - [initialize,98] - Located property source: CompositePropertySource {name='consul', propertySources=[ConsulPropertySource {name='config/sdb-graph-service/'}, ConsulPropertySource {name='config/application/'}]}
09:49:23.772 [main] INFO  cn.sciencedb.GraphApplication - [logStartupProfileInfo,675] - No active profile set, falling back to default profiles: default
09:49:24.791 [main] INFO  org.springframework.data.repository.config.RepositoryConfigurationDelegate - [multipleStoresDetected,244] - Multiple Spring Data modules found, entering strict repository configuration mode!
09:49:24.792 [main] INFO  org.springframework.data.repository.config.RepositoryConfigurationDelegate - [registerRepositoriesIn,126] - Bootstrapping Spring Data repositories in DEFAULT mode.
09:49:24.870 [main] INFO  org.springframework.data.repository.config.RepositoryConfigurationDelegate - [registerRepositoriesIn,182] - Finished Spring Data repository scanning in 72ms. Found 3 repository interfaces.
09:49:24.872 [main] INFO  org.springframework.data.repository.config.RepositoryConfigurationDelegate - [multipleStoresDetected,244] - Multiple Spring Data modules found, entering strict repository configuration mode!
09:49:24.872 [main] INFO  org.springframework.data.repository.config.RepositoryConfigurationDelegate - [registerRepositoriesIn,126] - Bootstrapping Spring Data repositories in DEFAULT mode.
09:49:24.884 [main] INFO  org.springframework.data.repository.config.RepositoryConfigurationDelegate - [registerRepositoriesIn,182] - Finished Spring Data repository scanning in 9ms. Found 2 repository interfaces.
09:49:24.885 [main] WARN  o.s.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext - [refresh,557] - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.support.BeanDefinitionOverrideException: Invalid bean definition with name 'mongoMappingContext' defined in class path resource [cn/sciencedb/config/MongoConfig4.class]: Cannot register bean definition [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=mongoConfig4; factoryMethodName=mongoMappingContext; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [cn/sciencedb/config/MongoConfig4.class]] for bean 'mongoMappingContext': There is already [Root bean: class [org.springframework.data.mongodb.core.mapping.MongoMappingContext]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] bound.
09:49:24.894 [main] INFO  o.s.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener - [logMessage,142] - 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
09:49:24.897 [main] ERROR org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter - [report,42] - 

***************************
APPLICATION FAILED TO START
***************************

Description:

The bean 'mongoMappingContext', defined in class path resource [cn/sciencedb/config/MongodbConfig.class], could not be registered. A bean with that name has already been defined in null and overriding is disabled.

Action:

Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true

MongoConfig配置如下:

package cn.sciencedb.config;

import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.MongoTransactionManager;
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
import org.springframework.data.mongodb.core.convert.MongoConverter;
import org.springframework.data.mongodb.gridfs.GridFsTemplate;

@Configuration
public class MongodbConfig extends AbstractMongoConfiguration{

    @Value("${spring.data.mongodb.uri}")
    private String mongoUri;
    @Autowired
    private MongoConverter mongoConverter;

    @Bean
    GridFsTemplate gridFsTemplate(MongoDbFactory dbFactory) {
        return new GridFsTemplate(dbFactory, mongoConverter);
    }

    @Bean
    MongoTransactionManager transactionManager(MongoDbFactory dbFactory) {
        return new MongoTransactionManager(dbFactory);
    }

    @Override
    @Bean
    public MongoClient mongoClient() {
        MongoClientURI uri = new MongoClientURI(mongoUri);
        return new MongoClient(uri);
    }

    @Override
    protected String getDatabaseName() {
        //String dataBase = mongoUri.substring(mongoUri.lastIndexOf("/") + 1);
        return "sdb";
    }
}

你可能感兴趣的:(MongoConfig配置文件存在错误)