Injection par mutateur et perte de valeur
Bonjour,
je code actuellement avec Spring et j'ai été amené à utiliser des injections de beans par mutateur dont la création fonctionne bien par contre leur utilisation posent problème puisqu'elle me génère un NullPointerException.
Pouvez-vous m'éclairer s'il vous plaît, merci d'avance ;) .
voici le code :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
@Controller
public class Home {
private ICoco daoA;
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
@RequestMapping(value = "", method = RequestMethod.GET)
public String home(Model model) {
System.out.println("coco : " + daoA.getNb()); // ici erreur null
return "home";
}
public void setDaoA(ICoco daoA){
System.out.println(" coco vaut : "+daoA.getNb()); // ici l'affichage se déroule correctement
this.daoA=daoA;
}
} |
la classe Coco :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
public class Coco implements ICoco{
private String nb = "5";
public Coco() {
super();
}
public Coco(String nb) {
super();
this.nb = nb;
}
public String getNb() {
return nb;
}
public void setNb(String nb) {
this.nb = nb;
}
} |
voici le fichier de configuration :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
<?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:context="http://www.springframework.org/schema/context"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<bean id="daoA" class="com.testmongo.mongo.model.Coco">
<property name="nb" value="John Doe"/>
</bean>
<bean id="metierZ" class="com.testmongo.mongo.Home">
<property name="daoA">
<ref bean="daoA" />
</property>
</bean>
</beans> |