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

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>
    

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

    打赏 评论
  • beyondSelf 2017-04-20 07:04

    ShardedJedis shardedJedis = redisDataSource.getRedisClient();

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


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

    打赏 评论
  • beyondSelf 2017-04-20 07:17



     <bean id="redisClientTemplate" class="base.redis.RedisClientTemplateImpl">
        <property name="redisDataSource" ref="redisDataSource"/>
    </bean>
    
    打赏 评论
  • uSeeWhat 2017-04-20 07:47

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

    打赏 评论
  • uSeeWhat 2017-04-20 07:49

    这是我的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

    打赏 评论

相关推荐 更多相似问题