升级后,应用程序启动时Spring数据mongodb崩溃

时间:2017-04-28 01:46:34

标签: mongodb spring-data spring-data-mongodb

在tomcat中启动应用程序我收到以下错误:

Caused by: java.lang.annotation.AnnotationFormatError: Invalid default: public abstract java.lang.Class org.springframework.data.mongodb.repository.config.EnableMongoRepositories.repositoryBaseClass()
    at java.lang.reflect.Method.getDefaultValue(Method.java:612)
    at org.springframework.core.annotation.AnnotationUtils.registerDefaultValues(AnnotationUtils.java:1178)
    at org.springframework.core.type.classreading.RecursiveAnnotationAttributesVisitor.visitEnd(RecursiveAnnotationAttributesVisitor.java:42)
    at org.springframework.core.type.classreading.AnnotationAttributesReadingVisitor.visitEnd(AnnotationAttributesReadingVisitor.java:64)
    at org.springframework.asm.ClassReader.readAnnotationValues(ClassReader.java:1869)
    at org.springframework.asm.ClassReader.accept(ClassReader.java:657)
    at org.springframework.asm.ClassReader.accept(ClassReader.java:523)
    at org.springframework.core.type.classreading.SimpleMetadataReader.<init>(SimpleMetadataReader.java:64)
    at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:98)
    at org.springframework.core.type.classreading.CachingMetadataReaderFactory.getMetadataReader(CachingMetadataReaderFactory.java:102)
    at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.findCandidateComponents(ClassPathScanningCandidateComponentProvider.java:287)
    ... 50 more

这是pom:

<properties>
    <spring.version>4.3.8.RELEASE</spring.version>
    <spring.data.version>1.9.10.RELEASE</spring.data.version>
    <spring.data.commons.version>1.7.0.RELEASE</spring.data.commons.version>
    <spring.data.commons.core.version>1.4.1.RELEASE</spring.data.commons.core.version>
    <log4j.version>1.2.17</log4j.version>
    <gson.version>2.3.1</gson.version>
    <servlet.version>3.1.0</servlet.version>
    <bson.version>3.4.2</bson.version>
    <commons.fileupload.version>1.3.1</commons.fileupload.version>
    <okhttp.version>3.5.0</okhttp.version>
    <mongo.java.driver.version>3.4.2</mongo.java.driver.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

<dependencies>

    <!-- Spring -->

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <!-- JavaEE APIs -->

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-mongodb</artifactId>
        <version>${spring.data.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-commons</artifactId>
        <version>${spring.data.commons.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-commons-core</artifactId>
        <version>${spring.data.commons.core.version}</version>
    </dependency>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>${gson.version}</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>${servlet.version}</version>
    </dependency>
    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>${commons.fileupload.version}</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>${log4j.version}</version>
    </dependency>
    <dependency>
        <groupId>com.squareup.okhttp3</groupId>
        <artifactId>okhttp</artifactId>
        <version>${okhttp.version}</version>
    </dependency>
    <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongo-java-driver</artifactId>
        <version>${mongo.java.driver.version}</version>
    </dependency>
    <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>bson</artifactId>
        <version>${bson.version}</version>
    </dependency>
</dependencies>

这是配置文件:

package com.somes.sns.persistence;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;

import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;

@Configuration
@PropertySource("classpath:mongodb.properties")
@EnableMongoRepositories("com.somes.sns.persistence.repository")
public class PersistenceConfig extends AbstractMongoConfiguration
{
    private static final String MONGODB_HOST = "mongodb.host";
    private static final String MONGODB_PORT = "mongodb.port";

    @Autowired
    Environment env;

    @Override
    protected String getDatabaseName()
    {
        return "MyDatabase";
    }

    @Override
    @Bean
    public MongoClient mongo() throws Exception
    {
        String mongoHost = env.getProperty(MONGODB_HOST);
        String mongoPort = env.getProperty(MONGODB_PORT);
        MongoClientURI mongoUri = new MongoClientURI("mongodb://" + mongoHost + ":" + mongoPort);
        MongoClient client = new MongoClient(mongoUri);
        client.setWriteConcern(WriteConcern.SAFE);
        return client;
    }

    @Override
    protected String getMappingBasePackage()
    {
        return "com.somes.sns.persistence.domain";
    }

    @Override
    @Bean
    public MongoTemplate mongoTemplate() throws Exception
    {
        return new MongoTemplate(mongo(), getDatabaseName());
    }
}

过去一直有效,直到我升级到1.9。什么可能导致此错误? 任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

我怀疑你有不兼容的Spring Framework和Spring Data版本。

请使用https://maven.apache.org/plugins/maven-dependency-plugin/examples/resolving-conflicts-using-the-dependency-tree.html

检查您的依赖项中是否存在冲突

升级到Spring Data 1.10.4应该与Spring Framework 2.3.8兼容。 或者,将Spring Framework降级到4.2.9应该也可以。

我还建议不要将Spring Data Commons明确指定为依赖项,因为无论如何它都会被作为一个瞬态依赖项引入。这样可以减少一个版本号。

另外:您几乎肯定不希望spring-data-commons-core此工件已过时,并且没有新版本。