java.lang.annotation.AnnotationFormatError 在 Spring Redis 集成中

java.lang.annotation.AnnotationFormatError in Spring Redis integration

我开发了一个库 (smartconnect-commons),它使用 spring-data-redis 与 redis 缓存集成。这个库可以插入任何想要使用 redis 的应用程序。该库中的 JUnit 测试成功。目前没有问题。

现在我在 Spring 启动应用程序中使用上述库作为依赖项。当 运行 spring 引导应用程序时,它因应用程序上下文初始化错误而失败。

2017-08-17 10:38:25,118 [main] ERROR org.springframework.boot.SpringApplication -  - Application startup failed
java.lang.annotation.AnnotationFormatError: Invalid default: public abstract java.lang.Class org.springframework.data.redis.repository.configuration.EnableRedisRepositories.repositoryFactoryBeanClass()
    at java.lang.reflect.Method.getDefaultValue(Method.java:612)
    at sun.reflect.annotation.AnnotationType.<init>(AnnotationType.java:132)
    at sun.reflect.annotation.AnnotationType.getInstance(AnnotationType.java:85)
    at sun.reflect.annotation.AnnotationParser.parseAnnotation2(AnnotationParser.java:266)
    at sun.reflect.annotation.AnnotationParser.parseAnnotations2(AnnotationParser.java:120)
    at sun.reflect.annotation.AnnotationParser.parseAnnotations(AnnotationParser.java:72)
    at java.lang.Class.createAnnotationData(Class.java:3521)
    at java.lang.Class.annotationData(Class.java:3510)
    at java.lang.Class.getAnnotations(Class.java:3446)
    at org.springframework.core.type.StandardAnnotationMetadata.<init>(StandardAnnotationMetadata.java:68)
    at org.springframework.boot.autoconfigure.data.AbstractRepositoryConfigurationSourceSupport.getConfigurationSource(AbstractRepositoryConfigurationSourceSupport.java:65)
    at org.springframework.boot.autoconfigure.data.AbstractRepositoryConfigurationSourceSupport.registerBeanDefinitions(AbstractRepositoryConfigurationSourceSupport.java:58)
    at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsFromRegistrars(ConfigurationClassBeanDefinitionReader.java:354)
    at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForConfigurationClass(ConfigurationClassBeanDefinitionReader.java:143)
    at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitions(ConfigurationClassBeanDefinitionReader.java:116)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:320)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:228)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:270)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:93)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:686)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:524)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:314)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151)
    at com.adp.smartconnect.SmartconnectWPInitializer.main(SmartconnectWPInitializer.java:32)

看起来与 spring 依赖版本不匹配,我花了很多时间但无法弄清楚。请查看 spring 启动应用程序的依赖关系树。

[INFO] --- maven-dependency-plugin:2.10:tree (default-cli) @ smartconnect-wage-payments-writer ---
[INFO] com.adp.smartconnect:smartconnect-wage-payments-writer:war:1.0.0-SNAPSHOT
[INFO] +- com.adp.smartconnect:smartconnect-config:jar:2.3.42:compile
[INFO] |  +- org.mule.modules:mule-module-mongo:jar:5.0.999:compile
[INFO] |  \- org.mongodb:mongo-java-driver:jar:3.4.2:compile
[INFO] +- com.adp.smartconnect:smartconnect-commons:jar:1.0.9-SNAPSHOT:compile
[INFO] |  +- org.springframework:spring-web:jar:4.3.8.RELEASE:compile
[INFO] |  +- org.springframework.data:spring-data-redis:jar:1.8.3.RELEASE:compile
[INFO] |  |  +- org.springframework.data:spring-data-keyvalue:jar:1.2.3.RELEASE:compile
[INFO] |  |  +- org.springframework:spring-oxm:jar:4.3.8.RELEASE:compile
[INFO] |  |  +- org.springframework:spring-context-support:jar:4.3.8.RELEASE:compile
[INFO] |  |  \- org.slf4j:jcl-over-slf4j:jar:1.7.25:compile
[INFO] |  +- redis.clients:jedis:jar:2.9.0:compile
[INFO] |  |  \- org.apache.commons:commons-pool2:jar:2.4.2:compile
[INFO] |  +- org.springframework:spring-core:jar:4.3.8.RELEASE:compile
[INFO] |  +- org.springframework:spring-test:jar:4.3.8.RELEASE:compile
[INFO] |  +- org.springframework.data:spring-data-mongodb:jar:1.10.3.RELEASE:compile
[INFO] |  |  +- org.springframework:spring-expression:jar:4.3.8.RELEASE:compile
[INFO] |  |  \- org.springframework.data:spring-data-commons:jar:1.13.3.RELEASE:compile
[INFO] |  +- com.fasterxml.jackson.core:jackson-core:jar:2.8.8:compile
[INFO] |  +- org.json:json:jar:20140107:compile
[INFO] |  +- com.fasterxml.jackson.core:jackson-databind:jar:2.8.8:compile
[INFO] |  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.8.0:compile
[INFO] |  +- com.adp.avs.domain:cdm:jar:1.0.9:compile
[INFO] |  |  \- com.fasterxml.jackson.module:jackson-module-jsonSchema:jar:2.8.8:compile
[INFO] |  \- org.codehaus.jackson:jackson-mapper-asl:jar:1.9.13:compile
[INFO] |     \- org.codehaus.jackson:jackson-core-asl:jar:1.9.13:compile
[INFO] +- com.adp.smartconnect:avs-prod-payload:jar:2.3.3:compile
[INFO] |  +- commons-beanutils:commons-beanutils-core:jar:1.8.0:compile
[INFO] |  +- commons-collections:commons-collections:jar:3.2.2:compile
[INFO] |  +- io.swagger:swagger-annotations:jar:1.5.3:compile
[INFO] |  +- org.projectlombok:lombok:jar:1.16.16:compile
[INFO] |  +- com.adp.smartconnect:smartconnect-mongo-cdm-library-cdm-0.9:jar:1.0.90:compile
[INFO] |  |  +- org.apache.commons:commons-collections4:jar:4.1:compile
[INFO] |  |  \- org.mule.modules:smartconnect-tracking-service-connector-cdm-0.9:jar:1.0.90:compile
[INFO] |  |     \- org.mule.tools.devkit:mule-devkit-shade:jar:3.8.0:compile
[INFO] |  +- org.apache.logging.log4j:log4j-api:jar:2.7:compile
[INFO] |  +- org.apache.logging.log4j:log4j-core:jar:2.7:compile
[INFO] |  \- ch.qos.logback:logback-classic:jar:1.1.11:compile
[INFO] |     \- ch.qos.logback:logback-core:jar:1.1.11:compile
[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:1.5.3.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter:jar:1.5.3.RELEASE:compile
[INFO] |  |  +- org.springframework.boot:spring-boot:jar:1.5.3.RELEASE:compile
[INFO] |  |  +- org.springframework.boot:spring-boot-autoconfigure:jar:1.5.3.RELEASE:compile
[INFO] |  |  +- org.springframework.boot:spring-boot-starter-logging:jar:1.5.3.RELEASE:compile
[INFO] |  |  |  +- org.slf4j:jul-to-slf4j:jar:1.7.25:compile
[INFO] |  |  |  \- org.slf4j:log4j-over-slf4j:jar:1.7.25:compile
[INFO] |  |  \- org.yaml:snakeyaml:jar:1.17:runtime
[INFO] |  +- org.springframework.boot:spring-boot-starter-tomcat:jar:1.5.3.RELEASE:compile
[INFO] |  |  +- org.apache.tomcat.embed:tomcat-embed-core:jar:8.5.14:compile
[INFO] |  |  +- org.apache.tomcat.embed:tomcat-embed-el:jar:8.5.14:compile
[INFO] |  |  \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:8.5.14:compile
[INFO] |  +- org.hibernate:hibernate-validator:jar:5.3.5.Final:compile
[INFO] |  |  +- javax.validation:validation-api:jar:1.1.0.Final:compile
[INFO] |  |  +- org.jboss.logging:jboss-logging:jar:3.3.1.Final:compile
[INFO] |  |  \- com.fasterxml:classmate:jar:1.3.3:compile
[INFO] |  \- org.springframework:spring-webmvc:jar:4.3.8.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-test:jar:1.5.3.RELEASE:test
[INFO] |  +- org.springframework.boot:spring-boot-test:jar:1.5.3.RELEASE:test
[INFO] |  +- org.springframework.boot:spring-boot-test-autoconfigure:jar:1.5.3.RELEASE:test
[INFO] |  +- com.jayway.jsonpath:json-path:jar:2.2.0:test
[INFO] |  |  \- net.minidev:json-smart:jar:2.2.1:test
[INFO] |  |     \- net.minidev:accessors-smart:jar:1.1:test
[INFO] |  |        \- org.ow2.asm:asm:jar:5.0.3:test
[INFO] |  +- org.assertj:assertj-core:jar:2.6.0:test
[INFO] |  +- org.mockito:mockito-core:jar:1.10.19:test
[INFO] |  |  \- org.objenesis:objenesis:jar:2.1:test
[INFO] |  +- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] |  +- org.hamcrest:hamcrest-library:jar:1.3:test
[INFO] |  \- org.skyscreamer:jsonassert:jar:1.4.0:test
[INFO] |     \- com.vaadin.external.google:android-json:jar:0.0.20131108.vaadin1:test
[INFO] +- com.typesafe.akka:akka-actor_2.11:jar:2.5.0:compile
[INFO] |  +- org.scala-lang:scala-library:jar:2.11.8:compile
[INFO] |  +- com.typesafe:config:jar:1.3.1:compile
[INFO] |  \- org.scala-lang.modules:scala-java8-compat_2.11:jar:0.7.0:compile
[INFO] +- com.typesafe.akka:akka-remote_2.11:jar:2.5.0:compile
[INFO] |  +- com.typesafe.akka:akka-stream_2.11:jar:2.5.0:compile
[INFO] |  |  +- org.reactivestreams:reactive-streams:jar:1.0.0:compile
[INFO] |  |  \- com.typesafe:ssl-config-core_2.11:jar:0.2.1:compile
[INFO] |  |     \- org.scala-lang.modules:scala-parser-combinators_2.11:jar:1.0.4:compile
[INFO] |  +- com.typesafe.akka:akka-protobuf_2.11:jar:2.5.0:compile
[INFO] |  +- io.netty:netty:jar:3.10.6.Final:compile
[INFO] |  +- io.aeron:aeron-driver:jar:1.2.3:compile
[INFO] |  \- io.aeron:aeron-client:jar:1.2.3:compile
[INFO] |     \- org.agrona:agrona:jar:0.9.3:compile
[INFO] +- com.typesafe.akka:akka-cluster_2.11:jar:2.5.0:compile
[INFO] +- com.typesafe.akka:akka-testkit_2.11:jar:2.5.0:compile
[INFO] +- com.typesafe.akka:akka-slf4j_2.11:jar:2.5.0:compile
[INFO] |  \- org.slf4j:slf4j-api:jar:1.7.25:compile
[INFO] +- javax.jms:javax.jms-api:jar:2.0:compile
[INFO] +- com.ibm.mq:mqallclient:jar:8.0:compile
[INFO] +- org.springframework:spring-jms:jar:4.3.0.RELEASE:compile
[INFO] |  +- org.springframework:spring-aop:jar:4.3.8.RELEASE:compile
[INFO] |  +- org.springframework:spring-beans:jar:4.3.8.RELEASE:compile
[INFO] |  +- org.springframework:spring-context:jar:4.3.8.RELEASE:compile
[INFO] |  +- org.springframework:spring-messaging:jar:4.3.8.RELEASE:compile
[INFO] |  \- org.springframework:spring-tx:jar:4.3.8.RELEASE:compile
[INFO] +- org.apache.commons:commons-lang3:jar:3.0:compile
[INFO] +- commons-io:commons-io:jar:2.5:compile
[INFO] +- commons-configuration:commons-configuration:jar:1.6:compile
[INFO] |  +- commons-lang:commons-lang:jar:2.4:compile
[INFO] |  +- commons-logging:commons-logging:jar:1.1.1:compile
[INFO] |  \- commons-digester:commons-digester:jar:2.1:compile
[INFO] |     \- commons-beanutils:commons-beanutils:jar:1.9.3:compile
[INFO] +- org.mockito:mockito-all:jar:1.10.19:test
[INFO] +- org.powermock:powermock-api-mockito:jar:1.7.0:test
[INFO] |  \- org.powermock:powermock-api-mockito-common:jar:1.7.0:test
[INFO] |     \- org.powermock:powermock-api-support:jar:1.7.0:test
[INFO] +- org.powermock:powermock-module-junit4:jar:1.7.0:test
[INFO] |  \- org.powermock:powermock-module-junit4-common:jar:1.7.0:test
[INFO] |     +- org.powermock:powermock-reflect:jar:1.7.0:test
[INFO] |     \- org.powermock:powermock-core:jar:1.7.0:test
[INFO] |        \- org.javassist:javassist:jar:3.21.0-GA:test
[INFO] \- junit:junit:jar:4.12:test

我观察到 spring-data-redis 模块无法与 spring boot 1.5.3 版本一起使用。所以当我将 spring 引导版本升级到 1.5.6 时,问题就解决了。

我遇到了同样的问题。

[ERROR] [org.springframework.boot.SpringApplication:771] Application startup failed java.lang.annotation.AnnotationFormatError: Invalid default: public abstract java.lang.Class org.springframework.data.redis.repository.configuration.EnableRedisRepositories.repositoryFactoryBeanClass()

那是因为我从git拉取了一些新代码,其中包括关于redis的代码,需要spring-boot-starter-data-redis库。 由于IDEA给出的问题,我按alt+enter自动导入这个库。

那么问题来了。 我终于点击了"In IDEA menu File -->Project Structure->Project Settings-->Libraries" 我发现这里的版本和build.gradle中的版本不一样。 我猜这个版本可能与 spring 引导的其他库不匹配。 所以我删除了这个库并尝试 运行 gradle 命令来构建项目。 我是这样解决问题的。