uSeeWhat 2017-04-20 06:34 采纳率: 100%
浏览 1919
已采纳

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");

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

  • 写回答

5条回答 默认 最新

  • beyondSelf 2017-04-21 06:49
    关注
     <bean id="redisDataSource" class="base.redis.RedisDataSourceImpl">
        <property name="shardedJedisPool"></property>
    </bean>
    <bean id="redisClientTemplate" class="base.redis.RedisClientTemplateImpl">
        <property name="redisDataSource"></property>
    </bean>
    

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化