NullPointerException avec @Autowired
Bonjour,
Je fais appelle à vous car je ne me sors pas de mon problème. Je pense qu'un mécanisme m'échappe mais je ne vois pas lequel.
Je cherche juste à injecter mes dépendances via les annotations mais cela me retourne toujours un NullPointerException.
Voici mon code
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| package com.librairIP.action;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.librairIP.service.UserManager;
import com.opensymphony.xwork2.ActionSupport;
@Component
public class ActionUser extends ActionSupport {
@Autowired
private UserManager userManager;
public String creerLivre(){
userManager.getName();
return SUCCESS;
}
} |
La classe action est appelée via Struts 2. C'est le userManager.getName() qui renvoi l'erreur car userManager est toujours Null.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| package com.librairIP.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.librairIP.dao.UserDao;
/**
* Implements business features to handler users
* @author xxx
*/
@Service
public class UserManagerImpl implements UserManager {
@Autowired
private UserDao userDao;
public void getName(){
userDao.getName();
}
} |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd">
<context:annotation-config />
<context:component-scan base-package="com.librairIP"/>
</beans> |
Merci de votre aide :)