Remember once jedis debugging

Remember once redis debugging

junit error

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)

Reference the
Spring5: @Autowired annotation, @ Resource annotations and notes @Service

Explanation.
Original link: https://www.cnblogs.com/szlbm/p/5512931.html

Reason:
jedisPool.getResource (); obtaining a null pointer exception is thrown
class JedisClientPool in
@Autowired
Private JedisPool jedisPool;
not set successfully, Causes
applicationContext-redis.xml code

<!-- 配置单机版的连接 -->
	<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" />
	

Later scanner configuration package the perfect solution, with the correct configuration code.

	<!-- 配置包扫描器 -->
<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;
	}
Published 15 original articles · won praise 0 · Views 506

Guess you like

Origin blog.csdn.net/qq_36335126/article/details/103730112