最近在用spring-boot編寫一個Lucene項目,中間用到了redis,引用了spring-boot-starter-data-redis,在eclipse中用外部Tomcat啟動項目一切正常,但是在運行Junit測試用例或使用spring-boot的application的main方法啟動項目時,會報如下錯誤:
1 2017-11-01 16:45:20.166 WARN 14252 --- [ main] o.s.w.c.s.GenericWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'buildController': Unsatisfied dependency expressed through field 'redisTemplate'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'redisConfig': Unsatisfied dependency expressed through field 'redisConnectionFactory'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration$RedisConnectionConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'spring.redis-org.springframework.boot.autoconfigure.data.redis.RedisProperties': Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.hibernate.validator.internal.engine.ConfigurationImpl 2 2017-11-01 16:45:20.206 INFO 14252 --- [ main] utoConfigurationReportLoggingInitializer : 3 4 Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled. 5 2017-11-01 16:45:20.230 ERROR 14252 --- [ main] o.s.boot.SpringApplication : Application startup failed
搜索堆棧中錯誤信息,很多博客都說是 hibernate-validator 版本沖突的問題:
http://www.jianshu.com/p/a33518f4012f
於是嘗試在pom文件中exclude排除spring-boot中自帶的這個jar包:
在pom.xml代碼里的配置變為如下:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> </exclusion> </exclusions> </dependency>
解決。