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 :

Spring 3.3. Configuration [Framework]


Sujet :

Spring Java

  1. #1
    Membre émérite Avatar de noOneIsInnocent
    Homme Profil pro
    Inscrit en
    Mai 2002
    Messages
    1 037
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations professionnelles :
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mai 2002
    Messages : 1 037
    Par défaut Spring 3.3. Configuration
    Bonjour

    J'utilise Spring 3.3 et j'ai un problème pour récupérer dans une classe Java une properties qui est dans un fichier de configuration

    Voici ce que j'ai mis dans mon fichier 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:util="http://www.springframework.org/schema/util"
        xmlns:ftp="http://www.springframework.org/schema/integration/ftp"
        xmlns:si="http://www.springframework.org/schema/integration"
        xmlns:batch="http://www.springframework.org/schema/batch"
        xmlns:task="http://www.springframework.org/schema/task"
        xmlns:jaxws="http://cxf.apache.org/jaxws"
        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/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
            http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.1.xsd 
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
            http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.1.xsd
            http://www.springframework.org/schema/integration/ftp http://www.springframework.org/schema/integration/ftp/spring-integration-ftp-2.1.xsd
            http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
     
        <context:annotation-config />
        <context:component-scan base-package="my.package" />
     
        <context:property-placeholder location="file:///${PATH_CONFIGURATION}\file.properties"/> 
    ....
    et ma classe Java

    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
     
    public class MyClass {
     
        private Logger logger = LoggerFactory
                .getLogger(MyClass .class);
     
      @Value("${my.key}")
        private String myProperrties;
     
        public void myMethod() {
        logger.debug("myProperrties:" + myProperrties);
     
        }
     
     
    }
    et dans le log de ma méthode myMethod j'ai la trace myProperrties: null

    J'ai essayé plusieurs autres méthodes mais sans succès. Notamment en déclarant le bean dans mon fichier XML mais là aussi j'ai toujours une valeur nulle

    Si quelqu'un à une idée je suis preneur
    Merci

  2. #2
    Expert confirmé
    Avatar de sinok
    Profil pro
    Inscrit en
    Août 2004
    Messages
    8 765
    Détails du profil
    Informations personnelles :
    Âge : 45
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Août 2004
    Messages : 8 765
    Par défaut
    Euh, c'est quoi ce backslash dans la location de ton property-placeholder?

  3. #3
    Membre émérite Avatar de noOneIsInnocent
    Homme Profil pro
    Inscrit en
    Mai 2002
    Messages
    1 037
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations professionnelles :
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mai 2002
    Messages : 1 037
    Par défaut
    merci d'avoir jeter un oeil sur le problème
    C'est parce que mon environnement est sous windows
    j'ai essayé le slash mais visiblement le fichier n'a pas été trouvé au démarrage de l'application

    Autre précision c'est une application web

  4. #4
    Expert confirmé
    Avatar de sinok
    Profil pro
    Inscrit en
    Août 2004
    Messages
    8 765
    Détails du profil
    Informations personnelles :
    Âge : 45
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Août 2004
    Messages : 8 765
    Par défaut
    Ce ne serait pas ton ${PATH_CONFIGURATION} qui serait mal alimenté par le plus grand des hasards.
    Normalement, on évite les backslash dans les URL par définition. Le slash est le séparateur dans une URL.

  5. #5
    Membre chevronné
    Homme Profil pro
    Ed Nat
    Inscrit en
    Janvier 2013
    Messages
    325
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Calvados (Basse Normandie)

    Informations professionnelles :
    Activité : Ed Nat
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Janvier 2013
    Messages : 325
    Par défaut
    Bonjour,
    tu roules les rr dans tes fichiers java...
    Plus sérieusement, que contient file.properties ?
    Y a-t-il une clé my.key ?

  6. #6
    Membre chevronné
    Homme Profil pro
    Ed Nat
    Inscrit en
    Janvier 2013
    Messages
    325
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Calvados (Basse Normandie)

    Informations professionnelles :
    Activité : Ed Nat
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Janvier 2013
    Messages : 325
    Par défaut
    Sinon, il serait peut-être plus sage de mettre un path de location relatif au classPath, surtout pour une appli web
    En créant un dossier (propertiesFolder) pour y ranger les propertiesFiles et en mettant :
    <context:property-placeholder location="classpath:propertiesFolder/file.properties"/>

  7. #7
    Membre émérite Avatar de noOneIsInnocent
    Homme Profil pro
    Inscrit en
    Mai 2002
    Messages
    1 037
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations professionnelles :
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mai 2002
    Messages : 1 037
    Par défaut
    Alors voici les réponses:

    - j'ai mis un / ça ne change rien
    - l'objectif est d'externaliser le fichier de conf
    - concernant le ${PATH_CONFIGURATION} il est bien alimenté. C'est une variable d'environnement windows



  8. #8
    Membre confirmé
    Homme Profil pro
    Développeur Java
    Inscrit en
    Octobre 2009
    Messages
    169
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur Java

    Informations forums :
    Inscription : Octobre 2009
    Messages : 169
    Par défaut
    Bonjour,

    moi j'ai :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     
    <bean name="propertyPlaceholder"
    		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    		<property name="locations">
    			<list>
    				<value>classpath:META-INF/database.properties</value>
    				<value>classpath:META-INF/security.properties</value>
    				<value>classpath:META-INF/monitoring.properties</value>
    			</list>
    		</property>
    	</bean>
    et dans ma class :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    @Value("${monitoring.instance}")
    private String instance ;
    et cela fonctionne

  9. #9
    Membre émérite Avatar de noOneIsInnocent
    Homme Profil pro
    Inscrit en
    Mai 2002
    Messages
    1 037
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations professionnelles :
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mai 2002
    Messages : 1 037
    Par défaut
    Merci @HadanMary
    Mais comment tu fais pour externaliser ta conf dans ton bean ?

  10. #10
    Membre confirmé
    Homme Profil pro
    Développeur Java
    Inscrit en
    Octobre 2009
    Messages
    169
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur Java

    Informations forums :
    Inscription : Octobre 2009
    Messages : 169
    Par défaut
    Désolé, mais je n'ai pas compris ta question.

  11. #11
    Membre émérite Avatar de noOneIsInnocent
    Homme Profil pro
    Inscrit en
    Mai 2002
    Messages
    1 037
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations professionnelles :
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mai 2002
    Messages : 1 037
    Par défaut
    je voudrais savoir quelle est la syntaxe de déclaration du bean dans le fichier applicationContext.xml pour que le fichier soit externalisé du war

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
      <bean name="propertyPlaceholder" 		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 		<property name="locations"> 			<list> 				<value>classpath:META-INF/database.properties</value> 				<value>classpath:META-INF/security.properties</value> 				<value>classpath:META-INF/monitoring.properties</value> 			</list> 		</property> 	</bean>
    dans ce que tua as mis tes fichiers de config sont dans le repertoire META-INF
    Je voudrais les déplacer dans unrepertoire en dehors du war du style c:\myApp\configuration avec bien sûr comme variable d'environnement

  12. #12
    Membre chevronné
    Homme Profil pro
    Ed Nat
    Inscrit en
    Janvier 2013
    Messages
    325
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Calvados (Basse Normandie)

    Informations professionnelles :
    Activité : Ed Nat
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Janvier 2013
    Messages : 325
    Par défaut
    Avec ${PATH_CONFIGURATION} le fichier est bien trouvé, donc le problème ne vient-il pas de file.properties...
    Du coup, au risque de paraître un peu lourd, désolé, je réitère ma question :
    Quel est le contenu de file.properties ?
    Contient t-il une clé my.key ?

  13. #13
    Membre émérite Avatar de noOneIsInnocent
    Homme Profil pro
    Inscrit en
    Mai 2002
    Messages
    1 037
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations professionnelles :
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mai 2002
    Messages : 1 037
    Par défaut
    Citation Envoyé par kox2ee Voir le message
    Avec ${PATH_CONFIGURATION} le fichier est bien trouvé, donc le problème ne vient-il pas de file.properties...
    Du coup, au risque de paraître un peu lourd, désolé, je réitère ma question :
    Quel est le contenu de file.properties ?
    Contient t-il une clé my.key ?
    tu peux autant de question que tu veux l'essentiel est de faire avancer le machin...
    oui, la clé existe bien

  14. #14
    Membre chevronné
    Homme Profil pro
    Ed Nat
    Inscrit en
    Janvier 2013
    Messages
    325
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Calvados (Basse Normandie)

    Informations professionnelles :
    Activité : Ed Nat
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Janvier 2013
    Messages : 325
    Par défaut
    Et sans externalisation,
    avec le location défini par rapport au classPath par exemple, et le fichier file.properties localisé en conséquence, myProperrties contient t-elle la valeur attendue ?

  15. #15
    Membre émérite Avatar de noOneIsInnocent
    Homme Profil pro
    Inscrit en
    Mai 2002
    Messages
    1 037
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations professionnelles :
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mai 2002
    Messages : 1 037
    Par défaut
    le fait de mettre le fichier dans le classpath ne change rien
    Mais je m'en doutais car dans le même fichier applicationContext.xml je récupère des propriétés du fichier properties qui sont injectés dans un bean utilisé pour se connecter à ma base de données

    d'ailleurs c'est ça qui me rend chèvre. J'arrive à injecter la propriété dans un bean générique de connexion à ma bdd mais je n'arrive pas à récupérer l'info dans ma classe Java

  16. #16
    Expert confirmé
    Avatar de sinok
    Profil pro
    Inscrit en
    Août 2004
    Messages
    8 765
    Détails du profil
    Informations personnelles :
    Âge : 45
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Août 2004
    Messages : 8 765
    Par défaut
    Et un @Component comme annotation de ta classe ne changerait pas les choses? Histoire de signifier à Spring que ta classe doit être managée...

  17. #17
    Membre émérite Avatar de noOneIsInnocent
    Homme Profil pro
    Inscrit en
    Mai 2002
    Messages
    1 037
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations professionnelles :
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mai 2002
    Messages : 1 037
    Par défaut
    Ma classe est déjà annotée avec le tag @Component
    ça ne change rien

  18. #18
    Expert confirmé
    Avatar de sinok
    Profil pro
    Inscrit en
    Août 2004
    Messages
    8 765
    Détails du profil
    Informations personnelles :
    Âge : 45
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Août 2004
    Messages : 8 765
    Par défaut
    Pourrais tu nous mettre le code intégral de ta classe.

    Car l'exemple que tu nous as donné ne contient ni les annotations de classe, ni les imports, ni le package.

  19. #19
    Membre émérite Avatar de noOneIsInnocent
    Homme Profil pro
    Inscrit en
    Mai 2002
    Messages
    1 037
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations professionnelles :
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mai 2002
    Messages : 1 037
    Par défaut
    je simplifie le code car c'est professionnel

    mais le principe 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
    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
     
    package com.myPackage;
     
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    @Component
    public class MyClass {
        private Logger logger = LoggerFactory
        .getLogger(SyncOrderRequestFactory.class);
    @Value("${my.Key}")
    private String myPropertie;
     
     
     
    public String getMyPropertie() {
        return myPropertie;
    }
     
     
    public void setMyPropertie(String myPropertie) {
        this.myPropertie = myPropertie;
    }
     
     
    public MyClass() {
     
    }
     
    public void publicMethod() {
        myMethod(new Object());
    }
     
    private void myMethod(Object _objParam) {
    logger.debug("INJECTION:"+this.myPropertie);
     
    }
     
    }
    dans le logger j'ai null

  20. #20
    Membre émérite Avatar de noOneIsInnocent
    Homme Profil pro
    Inscrit en
    Mai 2002
    Messages
    1 037
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations professionnelles :
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mai 2002
    Messages : 1 037
    Par défaut
    en plus dans les logs de démarrage j'ai

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    2013-01-31 17:56:30,756 DEBUG [pool-2-thread-1] - Class org.springframework.core.env.PropertySourcesPropertyResolver (line 76) - Found key 'my.propertie' in [localProperties] with type [String] and value 'MM/dd/yy'
    donc le conteneur la trouve mais je ne comprend pas pourquoi j'ai pas la valeur dans la classe

+ Répondre à la discussion
Cette discussion est résolue.
Page 1 sur 3 123 DernièreDernière

Discussions similaires

  1. [Framework] Spring 3.1 : configuration sans XML
    Par miles_ dans le forum Spring
    Réponses: 0
    Dernier message: 13/03/2012, 10h25
  2. Configurer un pakage dans spring config ?
    Par Invité dans le forum Spring
    Réponses: 1
    Dernier message: 28/12/2006, 19h11
  3. [Hibernate | Spring ] Configuration de fichier Spring
    Par DanZzz dans le forum Hibernate
    Réponses: 1
    Dernier message: 18/06/2006, 18h58
  4. Réponses: 10
    Dernier message: 31/05/2006, 16h15

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