这是抛出的异常
严重: 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");
都有注入为什么抛出空异常,试了很多方法都不行,求大神帮忙解答一下,多分