Spring整合Jedis抛空异常

这是抛出的异常
严重: Servlet.service() for servlet TestServlet threw exception
java.lang.NullPointerException
at base.redis.RedisClientTemplateImpl.set(RedisClientTemplateImpl.java:49)
at test.web.TestServlet.doPost(TestServlet.java:27)
at test.web.TestServlet.doGet(TestServlet.java:18)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:96)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)

下面贴出我的代码:

图片说明

spring-redis.xml

ignore-unresolvable="true" />

<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
    <property name="maxTotal">
    <value>${redis.pool.maxActive}</value>
    </property>
    <property name="maxIdle">
     <value>${redis.pool.maxIdle}</value>
    </property>
    <property name="testOnBorrow" value="true"/>
    <property name="testOnReturn" value="true"/>
</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.uri}" />
            </bean>
        </list>
    </constructor-arg>
</bean>
<bean id="redisDataSource" class="base.redis.RedisDataSourceImpl">
    <property name="shardedJedisPool"></property>
</bean>
<bean id="redisClientTemplate" class="base.redis.RedisClientTemplateImpl">
    <property name="redisDataSource"></property>
</bean>

RedisDataSourseImpl
private static final Logger log = Logger.getLogger(RedisDataSourceImpl.class);

private ShardedJedisPool shardedJedisPool;

public ShardedJedisPool getShardedJedisPool() {
return shardedJedisPool;
}
public void setShardedJedisPool(ShardedJedisPool shardedJedisPool) {
this.shardedJedisPool = shardedJedisPool;
}
/**

  • @return ShardedJedis
  • <获取redis客户端连接,执行命令> / public ShardedJedis getRedisClient() { System.out.println("执行getResource()前"); try { ShardedJedis shardedJedis = shardedJedisPool.getResource(); System.out.println("执行getResource()后"); return shardedJedis; } catch (Exception e) { log.error("获取redis客户端连接失败",e); } return null; } /*
  • @param shardedJedis
  • <将资源返还个pool>
    */
    public void returnResource(ShardedJedis shardedJedis) {

    shardedJedisPool.close();

}
/**

  • @param shardedJedis
  • <出现异常后,将资源返还给pool>
    */
    public void returnResource(ShardedJedis shardedJedis, boolean broken) {

    if(broken){
    shardedJedisPool.close();
    }else{
    shardedJedisPool.close();
    }

}

RedisClientTemplateImpl
private static final Logger log = Logger.getLogger(RedisClientTemplateImpl.class);

private RedisDataSource redisDataSource;

public RedisDataSource getRedisDataSource() {
return redisDataSource;
}
public void setRedisDataSource(RedisDataSource redisDataSource) {
this.redisDataSource = redisDataSource;
}
public void disconnect() {
System.out.println("disconnect()断开连接");
ShardedJedis shardedJedis = redisDataSource.getRedisClient();
shardedJedis.disconnect();
}
public String set(String key, String value) {
String result = null;
System.out.println("set方法");
ShardedJedis shardedJedis = redisDataSource.getRedisClient();
System.out.println("getRedisClient()执行后");
if (shardedJedis == null) {
return result;
}
boolean broken = false;
try {
result = shardedJedis.set(key, value);
} catch (Exception e) {
log.error(e.getMessage(), e);
broken = true;
} finally {
redisDataSource.returnResource(shardedJedis, broken);
}
return result;
}
TestServlet测试方法
RedisClientTemplateImpl redisClient = new RedisClientTemplateImpl();
String value = redisClient.set("yaya", "123156644");
System.out.println("获取到的值:"+value);
response.sendRedirect("index.jsp");

都有注入为什么抛出空异常,试了很多方法都不行,求大神帮忙解答一下,多分

0

5个回答

 <bean id="redisDataSource" class="base.redis.RedisDataSourceImpl">
    <property name="shardedJedisPool"></property>
</bean>
<bean id="redisClientTemplate" class="base.redis.RedisClientTemplateImpl">
    <property name="redisDataSource"></property>
</bean>

你配置都存在这些问题,你可以调试下嘛,问题定位了很快可以解决

1
weixin_38119445
uSeeWhat 问题解决了,jedisPoolConfig这个用构造器注入不能使用name,还有其他一些细节问题包括jedis版本啊,新研究的redis,谢谢指教啊
2 年多之前 回复

ShardedJedis shardedJedis = redisDataSource.getRedisClient();

从代码上来看抛空指针异常的只可能是这,同时看你配置文件


这redisDataSource并没有初始化,所以空指针很正常

1



 <bean id="redisClientTemplate" class="base.redis.RedisClientTemplateImpl">
    <property name="redisDataSource" ref="redisDataSource"/>
</bean>
1
weixin_38119445
uSeeWhat 如果这里不容易看,可以加我微信1104929008我给你源码,帮我看一下
2 年多之前 回复
weixin_38119445
uSeeWhat 我照你的改了,加了ref还是一样啊
2 年多之前 回复

有人帮忙解答一下吗?谢谢啦

0

这是我的redis.properties

redis.pool.maxActive=200
redis.pool.maxIdle=50
redis.pool.minIdle=10
redis.pool.maxWaitMillis=20000
redis.pool.maxWait=300
redis.uri = redis://:password@127.0.0.1:6379/0
redis.timeout=30000

0
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!