Java @向对象dao注入adnotation并给定null
当我尝试在服务文件中注入对象dao时,会出现空指针异常。 这是我的密码: web.xml:Java @向对象dao注入adnotation并给定null,java,spring,spring-mvc,Java,Spring,Spring Mvc,当我尝试在服务文件中注入对象dao时,会出现空指针异常。 这是我的密码: web.xml: <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/applicationContext.xml, classpath:/hibernateContext.xml</param-value> </context-para
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/applicationContext.xml, classpath:/hibernateContext.xml</param-value>
</context-param>
AccountServiceImpl.java:
@Service("accountService")
@Transactional(readOnly = true)
public class AccountServiceImpl implements AccountService {
@Inject
private UserDAO userDao;
@Transactional(readOnly = false)
public boolean registerAccount(User user, Errors errors) {
...
userDao.create(user);
...
当涉及AccountService中的registerAccount方法时,停止在userDao.create()。此时的UserDao为NULL。为什么?我试着用@Autowired而不是@Inject,但没用:(
提前感谢您的帮助。不,具有
@Autowired
或@Inject
目标的已处理bean不能向该目标注入null
值。如果无法找到候选bean,Spring将抛出异常
您必须自己创建对象,或者它是由Spring以外的容器创建的。谢谢您的回答。问题已解决。我忘记为userDao添加@Repository adnotation。@不客气。您在哪里创建
AccountServiceImpl
对象?
@Controller
@RequestMapping("/users")
public class AccountController {
@Autowired
private AccountService accountService;
public String postRegistrionForm(...) {
accountService.registerAccount(...);
}
@Service("accountService")
@Transactional(readOnly = true)
public class AccountServiceImpl implements AccountService {
@Inject
private UserDAO userDao;
@Transactional(readOnly = false)
public boolean registerAccount(User user, Errors errors) {
...
userDao.create(user);
...