IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Spring Java Discussion :

Partage context inter-projet


Sujet :

Spring Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre très actif
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Avril 2009
    Messages
    391
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Avril 2009
    Messages : 391
    Par défaut Partage context inter-projet
    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 :

    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>
    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
     
    	   ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
     
    	   MdpService mdpService = (MdpService) context.getBean("mdpServiceImpl");
     
    	   Mdp mdp = mdpService.recupererMdp(new Utilisateur());
     
    	   System.out.println(mdp.getMdp());
    Bon, là c'est de la lecture mais l'insertion fonctionne aussi .

    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 :

    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>
    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
    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>
    Comme vous pouvez le constater je fais un <import resource="..." />, mais cela ne semble pas suffire.

    Et enfin j'ai mon Web service :

    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);
    	}
    }
    J'instancie mon Service, je créer mes trois modèles et j'appelle l'une de mes méthodes de mon projet BUSINESS.

    Le Service de BUSINESS est le suivant :

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

    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.

  2. #2
    Membre très actif
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Avril 2009
    Messages
    391
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Avril 2009
    Messages : 391
    Par défaut
    Problème résolu, il fallait bien mettre les annotations merci quand même.

  3. #3
    Membre éclairé Avatar de domiq44
    Homme Profil pro
    Inscrit en
    Novembre 2005
    Messages
    302
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations forums :
    Inscription : Novembre 2005
    Messages : 302
    Par défaut
    Bonsoir,

    C'est sympa d'avoir donné la solution. Merci.

    Mais ça aurait été encore plus sympa d'ajouter le bout de code modifié afin d'en faire profiter tout le monde.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [VB.NET] Question de béotien : partage de données inter - projet
    Par CaptainJuv dans le forum Débuter
    Réponses: 0
    Dernier message: 24/07/2014, 13h32
  2. [WD17] GDS : éléments partagé entre plusieurs projets
    Par R&B dans le forum WinDev
    Réponses: 3
    Dernier message: 23/05/2013, 14h58
  3. Partager ressources entre projets
    Par Gaetch dans le forum Eclipse Java
    Réponses: 5
    Dernier message: 15/04/2011, 14h12
  4. Relation inter projets
    Par aure298 dans le forum C#
    Réponses: 4
    Dernier message: 08/10/2008, 02h46
  5. [VS2003/VS2005] Partage éléments entre projets
    Par Cereal123 dans le forum Visual Studio
    Réponses: 3
    Dernier message: 27/09/2006, 17h22

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo