Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java @向对象dao注入adnotation并给定null_Java_Spring_Spring Mvc - Fatal编程技术网

Java @向对象dao注入adnotation并给定null

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

当我尝试在服务文件中注入对象dao时,会出现空指针异常。 这是我的密码:

web.xml:

<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);
        ...