版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/LuuvyJune/article/details/81456143
接上一篇https://blog.csdn.net/LuuvyJune/article/details/81390206
服务端用Autowired注入service进行查询操作,发现userservice为空
public class LoginService {
@Autowired
UserService userService;
/**
* 业务系统传来token
* @param token 判断用户是否有认证凭据--ticket(认证中心生成)
* @return username,userId,登陆时间
*/
public UserInfo getUserInfo(String token){
String username = "";
UserInfo userInfo = new UserInfo();
if (null != token && !"".equals(token)) {
//如果token不为空,从缓存中获取username
username = RedisCache.get(token);
if(null!=username && !"".equals(username)){
//根据账户查询userid
User user = userService.queryUserByLoginName(username);
userInfo.setUserId(user.getId());
userInfo.setUserName(username);
//设置当前登录时间
userInfo.setLoginTime(System.currentTimeMillis());
}
}
return userInfo;
}
}
解决方案:
注入service换一种方案:
private static WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext();
UserService userService =(UserService) context.getBean("userService");
搞定!