spring+redis 报NullPointerException异常

lxraiyl 2015-05-29 10:31:26
小弟在使用Spring的spring-data-redis来操作redis缓存时遇到下面这个问题,麻烦各位大侠帮助解惑一下:
application.xml配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.21.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.1.xsd">

<!-- 开启缓存注解 -->
<cache:annotation-driven cache-manager="cacheManager"/>

<!-- 自动对标注@Autowired的Bean自动注入 -->
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>


<context:property-placeholder location="classpath:redis.properties" />

<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<!-- <property name="maxActive" value="${redis.maxActive}" />
<property name="maxWait" value="${redis.maxWait}" /> -->
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean>

<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="${redis.host}" p:port="${redis.port}" p:pool-config-ref="poolConfig"/>

<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
</bean>

<!-- 缓存管理器: 使用redis 当做缓存 -->
<bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager" c:template-ref ="redisTemplate"/>

<bean id="userServiceImpl" class="yyuap.redis.spring.service.UserServiceImpl"></bean>
</beans>



UserServiceImpl类如下:

package yyuap.redis.spring.service;

import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.util.StringUtils;

import yyuap.redis.spring.model.UserEntity;

/**
* 业务层接口实现
* @author leo
*
*/
public class UserServiceImpl {

private static final String cacheKey ="userEntity";

/**
* 新增
* @param entity
* @return
* @CachePut 这个注释可以确保方法被执行,同时方法的返回值也被记录到缓存中,实现缓存与数据库的同步更新。
*/
@CachePut(key ="#entity.getUserId()",value ="entity")
public void addUserEntity(UserEntity entity) {
//非空
if(entity ==null || StringUtils.isEmpty(entity.getUserId())){
}
/**
* 做数据库持久化,这里就无需再申明了
*/
System.out.println("先插入数据库中,.........");

}
}


UserEntiy实体类如下:

package yyuap.redis.spring.model;
/**
* 用户实体类
* @author leo
*
*/
public class UserEntity {
//用户id
private String userId;
//用户账号
private String EmpCode;
//用户名称
private String EmpName;
//用户角色
private String role;
//职位
private String title;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getEmpCode() {
return EmpCode;
}
public void setEmpCode(String empCode) {
EmpCode = empCode;
}
public String getEmpName() {
return EmpName;
}
public void setEmpName(String empName) {
EmpName = empName;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}


测试类如下:

package yyuap.redis.spring;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import yyuap.redis.spring.model.UserEntity;
import yyuap.redis.spring.service.UserServiceImpl;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext.xml"})
public class TestUserServiceImpl extends AbstractJUnit4SpringContextTests{

@Autowired
private UserServiceImpl userServiceImpl;

@Test
public void testAddUser(){
UserEntity entity = new UserEntity();
entity.setUserId("000003");
entity.setEmpCode("130566");
entity.setEmpName("leonardo-zeng");
entity.setRole("Java Development Engineer");
entity.setTitle("PM");
userServiceImpl.addUserEntity(entity);
}

}


但是在运行测试类的testAddUser()方法时总是报如下异常:


这是什么原因导致的。
...全文
1909 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_22627685 2017-07-20
  • 打赏
  • 举报
回复
我也遇到过这种情况,检查下service中的bean有没有注入
qq_33675088 2017-05-05
  • 打赏
  • 举报
回复
详细一点啊????
邹邹wl 2015-05-29
  • 打赏
  • 举报
回复
TestUserServiceImpl  这个的37行是什么?
engourdi 2015-05-29
  • 打赏
  • 举报
回复
把异常都贴出来吧
lxraiyl 2015-05-29
  • 打赏
  • 举报
回复
已解决。。。注解写错了
lxraiyl 2015-05-29
  • 打赏
  • 举报
回复
引用 2 楼 z345434645 的回复:
TestUserServiceImpl  这个的37行是什么?
就是调用UserServiceImpl的addUserEntiy()方法

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧