Bonjour à vous,
Je dispose d'un projet (projet BUSINESS) qui contient des DAO/Modeles/Services et qui communiquent avec ma BDD MySQL via Hibernate 5 géré par Spring 4.
Et je dispose d'un autre projet (projet WSREST) avec des web service, gérés par Spring MVC 4 (annotation @RestController etc...).
Mon problème est le suivant :
1/ Si je fais une classe Main de test dans mon projet BUSINESS avec un tentative d'insertion dans ma base, ça fonctionne.
J'ai une classe modèle annoter avec @Entity
Une classe DAO annotée avec @Service et @Autowired sur la SessionFactory d'hibernate
Et une classe Service elle aussi annotée avec @Service, un champs de classe @Autowired qui représente la DAO décrite précédemment et des méthodes annotées @Transactional
Et enfin, j'ai le fichier spring.xml dans mon dossier com/src/resource suivant :
Donc quand je lance le main suivant ça fonctionne :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 <?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:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <tx:annotation-driven/> <context:component-scan base-package="com.ecollection.dao"></context:component-scan> <context:component-scan base-package="com.ecollection.service"></context:component-scan> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3307/ecollection" /> <property name="username" value="root" /> <property name="password" value="" /> </bean> <bean id="hibernateAnnotatedSessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="annotatedClasses"> <list> <value>com.ecollection.model.Utilisateur</value> <value>com.ecollection.model.Perso</value> <value>com.ecollection.model.Mdp</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop> <prop key="current_session_context_class">thread</prop> <prop key="show_sql">false</prop> </props> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="sessionFactory" ref="hibernateAnnotatedSessionFactory"></property> </bean> </beans>
Bon, là c'est de la lecture mais l'insertion fonctionne aussi
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8 ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); MdpService mdpService = (MdpService) context.getBean("mdpServiceImpl"); Mdp mdp = mdpService.recupererMdp(new Utilisateur()); System.out.println(mdp.getMdp());.
2/ Maintenant quand je passe par mon WebService Rest, cela ne fonctionne plus et je me retrouve avec des NullPointerException (je pense que mes beans ne sont plus instancié).
Dans ce projet, j'ai mon web.xml :
Le fichier de configuration qui est appelé par ce web.xml
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>monprojet Rest</display-name> <!-- Le servlet name correspond au nom du fichier de configuration sans la partie -servlet.xml --> <servlet> <servlet-name>monprojet-rest</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>monprojet-rest</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>
Comme vous pouvez le constater je fais un <import resource="..." />, mais cela ne semble pas suffire.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 <?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:tx="http://www.springframework.org/schema/tx" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <import resource="classpath:spring.xml"/> <mvc:annotation-driven/> <context:component-scan base-package="monprojet.monprojet.rest"></context:component-scan> </beans>
Et enfin j'ai mon Web service :
J'instancie mon Service, je créer mes trois modèles et j'appelle l'une de mes méthodes de mon projet BUSINESS.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 @RestController @RequestMapping("/service") public class InscriptionWsRest { @RequestMapping(value = "/inscription", method = RequestMethod.POST) @ResponseStatus(value=HttpStatus.OK) public void inscription( @RequestBody InfosInscriptionDto infosInscription) { ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); InscriptionServiceImpl inscriptionServiceImpl = (InscriptionServiceImpl) context.getBean("inscriptionServiceImpl"); Utilisateur utilisateur = new Utilisateur(); Perso perso = new Perso(utilisateur, infosInscription); Mdp mdp = new Mdp(utilisateur, infosInscription.getMdp()); inscriptionServiceImpl.creerUtilisateur(utilisateur, perso, mdp); } }
Le Service de BUSINESS est le suivant :
Comme vous pouvez le voir, il n'y a pas d'annotation mais j'ai essayé avec, ça ne changeait rien et puisque j'appel d'autres services qui sont annoté, je ne pense pas que ce soit nécessaire.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 public class InscriptionServiceImpl implements InscriptionService { private UtilisateurServiceImpl utilisateurServiceImpl = new UtilisateurServiceImpl(); private PersoServiceImpl persoServiceImpl = new PersoServiceImpl(); private MdpServiceImpl mdpServiceImpl = new MdpServiceImpl(); public void creerUtilisateur(Utilisateur utilisateur, Perso perso, Mdp mdp) { utilisateurServiceImpl.creerUtilisateur(utilisateur); persoServiceImpl.creerPerso(perso); mdpServiceImpl.creerMdp(mdp); System.out.println("TEST"); } }
Voilà, lorsque je lance tout ça, les 3 services sont instanciés. Par contre, chacun de ces services contiennent un champ DAO @Autowired qui est à null...
Avez vous une idée ? Merci d'avance =D.
Partager