项目启动后,JedisConnection初始化失败
项目的日志为:
2016-25-07 22:25:42.891 [http-nio-8084-exec-4] [DEBUG] o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'wxServiceController' 2016-25-07 22:25:42.892 [http-nio-8084-exec-4] [DEBUG] o.s.d.r.core.RedisConnectionUtils - Opening RedisConnection 2016-25-07 22:25:42.898 [http-nio-8084-exec-4] [DEBUG] o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Resolving exception from handler [public com.elephant.dto.cardpack.WeiXinCreateCardPackEntity com.elephant.web.WxServiceController.createCardPack(javax.servlet.http.HttpServletRequest) throws java.lang.Exception]: org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.springframework.data.redis.connection.jedis.JedisConnection 2016-25-07 22:25:42.898 [http-nio-8084-exec-4] [DEBUG] o.s.w.s.m.a.ResponseStatusExceptionResolver - Resolving exception from handler [public com.elephant.dto.cardpack.WeiXinCreateCardPackEntity com.elephant.web.WxServiceController.createCardPack(javax.servlet.http.HttpServletRequest) throws java.lang.Exception]: org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.springframework.data.redis.connection.jedis.JedisConnection 2016-25-07 22:25:42.898 [http-nio-8084-exec-4] [DEBUG] o.s.w.s.m.s.DefaultHandlerExceptionResolver - Resolving exception from handler [public com.elephant.dto.cardpack.WeiXinCreateCardPackEntity com.elephant.web.WxServiceController.createCardPack(javax.servlet.http.HttpServletRequest) throws java.lang.Exception]: org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.springframework.data.redis.connection.jedis.JedisConnection 2016-25-07 22:25:42.899 [http-nio-8084-exec-4] [DEBUG] o.s.web.servlet.DispatcherServlet - Could not complete request org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.springframework.data.redis.connection.jedis.JedisConnection at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:978) ~[spring-webmvc-4.3.0.RELEASE.jar:4.3.0.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897) ~[spring-webmvc-4.3.0.RELEASE.jar:4.3.0.RELEASE] at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) [spring-webmvc-4.3.0.RELEASE.jar:4.3.0.RELEASE] at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872) [spring-webmvc-4.3.0.RELEASE.jar:4.3.0.RELEASE] at javax.servlet.http.HttpServlet.service(HttpServlet.java:648) [servlet-api.jar:na] at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) [spring-webmvc-4.3.0.RELEASE.jar:4.3.0.RELEASE] at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) [servlet-api.jar:na] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292) [catalina.jar:8.0.36] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) [catalina.jar:8.0.36] at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) [tomcat-websocket.jar:8.0.36] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) [catalina.jar:8.0.36] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) [catalina.jar:8.0.36] at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197) [spring-web-4.3.0.RELEASE.jar:4.3.0.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.0.RELEASE.jar:4.3.0.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) [catalina.jar:8.0.36] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) [catalina.jar:8.0.36] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212) [catalina.jar:8.0.36] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) [catalina.jar:8.0.36] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141) [catalina.jar:8.0.36] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) [catalina.jar:8.0.36] at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616) [catalina.jar:8.0.36] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) [catalina.jar:8.0.36] at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528) [catalina.jar:8.0.36] at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1099) [tomcat-coyote.jar:8.0.36] at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:670) [tomcat-coyote.jar:8.0.36] at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1520) [tomcat-coyote.jar:8.0.36] at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1476) [tomcat-coyote.jar:8.0.36] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_91] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_91] at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-util.jar:8.0.36] at java.lang.Thread.run(Thread.java:745) [na:1.8.0_91] Caused by: java.lang.NoClassDefFoundError: Could not initialize class org.springframework.data.redis.connection.jedis.JedisConnection at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.getConnection(JedisConnectionFactory.java:346) ~[spring-data-redis-1.7.2.RELEASE.jar:na] at org.springframework.data.redis.core.RedisConnectionUtils.doGetConnection(RedisConnectionUtils.java:128) ~[spring-data-redis-1.7.2.RELEASE.jar:na] at org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:91) ~[spring-data-redis-1.7.2.RELEASE.jar:na] at org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:78) ~[spring-data-redis-1.7.2.RELEASE.jar:na] at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:189) ~[spring-data-redis-1.7.2.RELEASE.jar:na] at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:164) ~[spring-data-redis-1.7.2.RELEASE.jar:na] at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:152) ~[spring-data-redis-1.7.2.RELEASE.jar:na] at com.elephant.redis.RedisServiceImpl.get(RedisServiceImpl.java:104) ~[classes/:na] at com.elephant.util.wx.WxConfigUtil.getAccessToken(WxConfigUtil.java:72) ~[classes/:na] at com.elephant.service.wx.impl.WxCardPackServiceImpl.createCardPack(WxCardPackServiceImpl.java:35) ~[classes/:na] at com.elephant.web.WxServiceController.createCardPack(WxServiceController.java:102) ~[classes/:na] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_91] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_91] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_91] at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_91] at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221) ~[spring-web-4.3.0.RELEASE.jar:4.3.0.RELEASE] at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136) ~[spring-web-4.3.0.RELEASE.jar:4.3.0.RELEASE] at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:114) ~[spring-webmvc-4.3.0.RELEASE.jar:4.3.0.RELEASE] at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) ~[spring-webmvc-4.3.0.RELEASE.jar:4.3.0.RELEASE] at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) ~[spring-webmvc-4.3.0.RELEASE.jar:4.3.0.RELEASE] at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) ~[spring-webmvc-4.3.0.RELEASE.jar:4.3.0.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963) ~[spring-webmvc-4.3.0.RELEASE.jar:4.3.0.RELEASE] ... 30 common frames omitted
redis配置为:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd "> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxIdle" value="${redis.maxIdle}"/> <property name="testOnBorrow" value="${redis.testOnBorrow}"/> </bean> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="usePool" value="true"></property> <property name="hostName" value="${redis.host}"/> <property name="port" value="${redis.port}"/> <property name="password" value="${redis.pass}"/> <constructor-arg index="0" ref="jedisPoolConfig"/> </bean> <bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool" scope="singleton"> <constructor-arg index="0" ref="jedisPoolConfig"/> <constructor-arg index="1"> <list> <bean class="redis.clients.jedis.JedisShardInfo"> <constructor-arg name="host" value="${redis.host}"/> <constructor-arg name="port" value="${redis.port}"/> <constructor-arg name="timeout" value="${redis.timeout}"/> <!--<constructor-arg name="weight" value="1" />--> </bean> </list> </constructor-arg> </bean> <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"> <property name="connectionFactory" ref="jedisConnectionFactory"/> </bean> <bean id="redisService" class="com.elephant.redis.RedisServiceImpl"></bean> </beans>
redis所使用的依赖:
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.4.2</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.3.0.RELEASE</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>org.redisson</groupId> <artifactId>redisson</artifactId> <version>2.2.24</version> </dependency>
redis封装类
public class RedisServiceImpl implements RedisService{ @Autowired private StringRedisTemplate redisTemplate; @Override public Boolean expire(String key, int seconds) { return redisTemplate.execute(new RedisCallback<Boolean>() { @Override public Boolean doInRedis(RedisConnection connection) throws DataAccessException { try { return connection.expire(key.getBytes(CHARSET), seconds); } catch (UnsupportedEncodingException e) { e.printStackTrace(); return false; } } }); } @Override public boolean set(String key, String value) { return redisTemplate.execute(new RedisCallback<Boolean>() { @Override public Boolean doInRedis(RedisConnection connection) throws DataAccessException { try { connection.set(key.getBytes(CHARSET), value.getBytes(CHARSET)); return true; } catch (UnsupportedEncodingException e) { e.printStackTrace(); return false; } } }); } @Override public boolean setNX(String key, String value) { return redisTemplate.execute(new RedisCallback<Boolean>() { @Override public Boolean doInRedis(RedisConnection connection) throws DataAccessException { try { return connection.setNX(key.getBytes(CHARSET), value.getBytes(CHARSET)); } catch (UnsupportedEncodingException e) { e.printStackTrace(); return false; } } }); } @Override public boolean set(String key, Object value) { String objectJson = JsonUtils.getInstance().toJsonString(value); return set(key, objectJson); } @Override public boolean del(String key) { return redisTemplate.execute(new RedisCallback<Boolean>() { @Override public Boolean doInRedis(RedisConnection connection) throws DataAccessException { try { connection.del(key.getBytes(CHARSET)); return true; } catch (UnsupportedEncodingException e) { e.printStackTrace(); return false; } } }); } @Override public <T> T get(String key, Class<T> clazz) { String value = get(key); return StringUtils.isBlank(value) ? null : JsonUtils.getInstance().fromJson(value, clazz); } @Override public String get(String key) { return redisTemplate.execute(new RedisCallback<String>() { @Override public String doInRedis(RedisConnection connection) throws DataAccessException { try { byte[] bs=connection.get(key.getBytes(CHARSET)); if(bs!=null) return new String(bs,CHARSET); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return null; } }); } @Override public BigInteger getSequence(String seqKey){ return redisTemplate.execute(new RedisCallback<BigInteger>() { @Override public BigInteger doInRedis(RedisConnection connection) throws DataAccessException { try { return BigInteger.valueOf(connection.incr(seqKey.getBytes(CHARSET))); } catch (UnsupportedEncodingException e) { e.printStackTrace(); return null; } } }); } }
请问是什么问题,怀疑是依赖版本的问题,但好像又不对。。。
我把你的依赖添加了运行说缺少commons-logging
谷歌了下,发现这个是依赖版本的问题,把依赖的版本改低就可以了