0
當試圖在服務文件中注入對象dao時,我得到一個空指針異常。 這裏是我的代碼:@Inject adnotation a dao null null
的web.xml:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/applicationContext.xml, classpath:/hibernateContext.xml</param-value>
</context-param>
的applicationContext.xml:
<context:component-scan base-package="com.projects.discussion"/>
AccountController.java:
@Controller
@RequestMapping("/users")
public class AccountController {
@Autowired
private AccountService accountService;
public String postRegistrionForm(...) {
accountService.registerAccount(...);
}
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代替@注入,但它並沒有幫助我:( 預先感謝您的幫助。
感謝您的回答。問題解決了。我忘了爲userDao添加@Repository adnotation – Piotr
@Piotr 別客氣。我們在哪裏創建一個'AccountServiceImpl'對象? –