@Inject 注入 还是报空指针
Posted 疯子加天才
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了@Inject 注入 还是报空指针相关的知识,希望对你有一定的参考价值。
@IocBean
@Service("userManagerService")
public class UserManagerServiceImpl implements UserManagerService{
@Inject private UserMastManagerBiz userMastManagerBiz;
public UserRegisterServiceResponseDTO addUser(
UserRegisterServiceRequestDTO requestDTO){
UserRegisterServiceResponseDTO responseDTO
= userMastManagerBiz.registerUserAccount(requestDTO);
return responseDTO;
}
}
结果 userMastManagerBiz 报空指针
@wendal 你的意思是UserMastManagerBiz 是手动new的,不是注入的吗?
要看 UserManagerServiceImpl 是怎么生成的, 问题是出在UserManagerServiceImpl 上
- 检查ioc的js文件, 看看有无重复声明, 同一个bean, 要么用js,要么用注解, 只能有一种生效
- 要是这个类的对象是直接new出来的,那就肯定没有"注入"这一服务了.
@wendal /*
* 本配置文件声明了整个应用的数据库连接部分。
*/
var ioc = {
dataSource : {
type : "com.alibaba.druid.pool.DruidDataSource",
fields : {
driverClass : "oracle.jdbc.driver.OracleDriver",
jdbcUrl : "jdbc:oracle:thin:@:1521:orcl",
user : "",
password : ""
}
},
dao : {
type : "org.nutz.dao.impl.NutDao",
args : [ {
refer : "dataSource"
} ]
}
};
我在js中没有声明bean,全都是以注入方式写的
这是我的nutz日志:
ALL Nutz Log via Log4jLogAdapter
[INFO ] [2016/04/02 11:04:59] SEE org.nutz.log.Logs - Nutz is licensed under the Apache License, Version 2.0 .
Report bugs : https://github.com/nutzam/nutz/issues
[INFO ] [2016/04/02 11:04:59] SEE org.nutz.mvc.NutFilter - NutFilter[nutz] starting ...
[INFO ] [2016/04/02 11:05:02] SEE org.nutz.mvc.impl.NutLoading - Nutz Version : 1.b.51
[INFO ] [2016/04/02 11:05:02] SEE org.nutz.mvc.impl.NutLoading - Nutz.Mvc[nutz] is initializing ...
[INFO ] [2016/04/02 11:05:02] SEE org.nutz.ioc.loader.annotation.AnnotationIocLoader - Scan complete ! Found 13 classes in 1 base-packages!
beans = ["customerCodeBiz", "home", "baseDao", "userMastManagerBiz", "complexDatabase", "mongoDao", "simpleDababase", "memcached", "baseBiz", "commonMongoService", "errorMessageContent", "userManagerServiceImpl", "baseMongo"]
[INFO ] [2016/04/02 11:05:02] SEE org.nutz.mvc.impl.NutLoading - Build URL mapping by org.nutz.mvc.impl.UrlMappingImpl ...
[WARN ] [2016/04/02 11:05:02] SEE org.nutz.mvc.impl.NutLoading - None module classes found!!!
[WARN ] [2016/04/02 11:05:02] SEE org.nutz.mvc.impl.NutLoading - None @At found in any modules class!!
[INFO ] [2016/04/02 11:05:02] SEE org.nutz.mvc.impl.NutLoading - Setup application...
[INFO ] [2016/04/02 11:05:02] SEE org.nutz.mvc.impl.NutLoading - Nutz.Mvc[nutz] is up in 528ms
这里是生成了这个userManagerServiceImpl 这个对象吧
@ds25 只是生成了定义,对象还没生成.
使用UserManagerServiceImpl 的代码贴一下, 估计是new UserManagerServiceImpl ()的, 而非从ioc容器拿.
@Controller
@RequestMapping("/home")
public class Home {
private Logger logger = Logger.getLogger(Home.class);
@Inject
private UserManagerServiceImpl userManagerService;
/**
* 个人日记-注册页面
*
* @return
* @Description:
*/
@RequestMapping(value = "/{site}/register" , method = RequestMethod.GET)
public ModelAndView register(HttpServletRequest request,
HttpServletResponse response) {
logger.info("进入注册页 register START");
ModelAndView mav = new ModelAndView();
mav.setViewName("homePage/registerPage");
logger.info("进入注册页 register End");
return mav;
}
/**
* 个人日记-正在注册
*
* @return
* @Description:
*/
@SuppressWarnings("unused")
@RequestMapping(value = "/{site}/registering" , method = RequestMethod.POST)
public ModelAndView registering(HttpServletRequest request,
@RequestParam("email") String email,
@RequestParam("username") String nickName,
@RequestParam("password") String password,
HttpServletResponse response) {
logger.info("正在注册 registering START");
ModelAndView mav = new ModelAndView();
mav.setViewName("homePage/registerPage");
email = email==null?"":email.trim();
nickName = nickName==null?"":nickName.trim();
password = password==null?"":password.trim();
UserRegisterServiceRequestDTO requestDTO = new UserRegisterServiceRequestDTO();
requestDTO.setEmail(email);
requestDTO.setNickName(nickName);
requestDTO.setPassword(password);
if(requestDTO != null){
userManagerService.addUser(requestDTO);
mav.setViewName("redirect:/home/homePage/logining.do");
}else{
mav.addObject("errorTips", "您输入数据有误!");
request.getSession().setAttribute("information", null);
}
logger.info("正在注册 registering End");
return mav;
}
}
spring会认nutz的注解????
@Inject
private UserManagerServiceImpl userManagerService;
这个绝对不会生效, 通过下面的方式取
Mvcs.ctx().getDefaultIoc(UserManagerServiceImpl.class)
@wendal 直接用Mvcs.ctx().getDefaultIoc(UserManagerServiceImpl.class)将
@Inject
private UserManagerServiceImpl userManagerService; 替代吗?
现在又出现一个问题:当我在类中直接使用
Mvcs.ctx().getDefaultIoc(UserManagerServiceImpl.class) 时,会出现
Multiple markers at this line
- Syntax error on token "ctx", Identifier expected after this
token
- Syntax error on token ".", { expected
请问这是什么原因?
@ds25 版本旧,改成 Mvcs.ctx.getDefaultIoc
改成 Mvcs.ctx.getDefaultIoc(UserManagerServiceImpl.class);
会出现
Syntax error on token "getDefaultIoc", Identifier expected after this token
这也是版本问题吗
@ds25 啥版本?
@wendal
打扰你很久了,可是改成
Mvcs.ctx.getDefaultIoc.get(UserManagerServiceImpl.class);
还是报Syntax error on tokens, delete these tokens
我用的是nutz-1.b.51.jar
@ds25 getDefaultIoc().get(XXXX.class)
getDefaultIoc是个方法啊,返回Ioc接口,然后再取bean
谢谢啦,麻烦你这么久,问题终于解决了。
学习nutz有段时间了,现在在做毕设,正在搭环境,不想用SSH,所以想用新学的nutz做一下,毕竟用SSH的同学太多了,也找个机会练练手,看看自己对nutz掌握的怎样了。
最后由衷说一声: 谢谢啦!!!!!
以上是关于@Inject 注入 还是报空指针的主要内容,如果未能解决你的问题,请参考以下文章
SpringBoot@Autowired注入静态成员变量报空指针异常
spring bean注入报空指针null,但set设值时对象是存在的,而且只是部分方法报空指针