记一次jedis调试

记一次redis调试

junit报错

java.lang.NullPointerException
at cn.e3mall.common.jedis.JedisClientPool.set(JedisClientPool.java:26)
at cn.e3mall.content.service.test.JedisTest.testJedisClient(JedisTest.java:73)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

参考了
Spring5:@Autowired注解、@Resource注解和@Service注解

的讲解。
原文链接: https://www.cnblogs.com/szlbm/p/5512931.html

原因:
jedisPool.getResource();获取了空指针,抛出了异常
类中JedisClientPool中的
@Autowired
private JedisPool jedisPool;
没有set成功,分析原因
applicationContext-redis.xml中代码

<!-- 配置单机版的连接 -->
	<bean id="jedisPool" class="redis.clients.jedis.JedisPool">
		<constructor-arg name="host" value="192.168.25.131" />
		<constructor-arg name="port" value="6379"/>
	</bean>
	
	<bean id="jedisClientPool" class="cn.e3mall.common.jedis.JedisClientPool" />
	

后来配置包扫描器完美解决,附正确配置代码。

	<!-- 配置包扫描器 -->
<context:component-scan base-package="cn.e3mall.common.jedis"/> 
	<!-- 配置单机版的连接 -->
	<bean id="jedisPool" class="redis.clients.jedis.JedisPool">
		<constructor-arg name="host" value="192.168.25.131" />
		<constructor-arg name="port" value="6379"/>
	</bean>
	
	<bean id="jedisClientPool" class="cn.e3mall.common.jedis.JedisClientPool" />
以上还可以这样修改:
不使用@Autowired注解,采用手工配置xml的方法,将jedisPool的值传入jedisClientPool中,只需要将代码改为如下即可:
   <bean id="jedisClientPool" class="cn.e3mall.common.jedis.JedisClientPool">
		<property name="jedisPool" ref="jedisPool"></property>
	</bean>
	<bean id="jedisPool" class="redis.clients.jedis.JedisPool">
		<constructor-arg name="host" value="192.168.25.131"/>
		<constructor-arg name="port" value="6379"/>
	</bean>
	//@Autowired 将该注解去掉,用get,set方法
	private JedisPool jedisPool;
    
	public JedisPool getJedisPool() {
		return jedisPool;
	}

	public void setJedisPool(JedisPool jedisPool) {
		this.jedisPool = jedisPool;
	}
发布了15 篇原创文章 · 获赞 0 · 访问量 506

猜你喜欢

转载自blog.csdn.net/qq_36335126/article/details/103730112
今日推荐