donc dans la classe on devrait avoir :
plutôt que ${my.Key}Code:
1
2
3 @Value("${my.propertie}") private String myPropertie;
non ?
Version imprimable
donc dans la classe on devrait avoir :
plutôt que ${my.Key}Code:
1
2
3 @Value("${my.propertie}") private String myPropertie;
non ?
:salut:
désolé de jouer les trouble-fêtes mais la version 3.3 existe pas à ce jour
Désolé pour la version je me suis trompé, c'est la 3.1
ok je vais tester avec la clé mais demain ....
merci encore
je vous tiens au courant
juste une remarque
je viens de trouver le lien suivant:
http://forum.springsource.org/showth...lderconfigurer
est-ce que quelqu'un peut me confirmer que le problème vient de la présence d'un point dans la clé de propriété ?
question bête peut être, mais tu as mis un setter sur ton champ annoté @Value?
je pensais que le @Value devait se mettre uniquement sur le membre de la classe ?
du coup c'est quoi la bonne façon ?
pour faire avancer le bouzin voici la log qui répond au problème
reste à trouvé comment mettre mon bean dans le contexteCode:
1
2Le Nom my.property n'est pas lié ce Contexte. Returning null.
si vous avez une idée
Bonjour,
non, le @Value("${valueKey}") se met bien sur la déclaration du membre.
Les accesseurs jouent ensuite juste leur rôle.
Par contre as tu essayé sans le dot, avec myProperty par exemple, et en modifiant également la clé dans le fichier file.properties ?
ou avec l'annotation @Value("${my['property']}") si la clé est toujours my.property
Et tu peux aussi essayer la déclaration plus classique des locations de bean dans applicationContext.xml comme le suggérait HadanMarv :
Code:
1
2
3
4
5
6
7
8 <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:file.properties</value> </list> </property> </bean>
j'ai essayé en mettant
et j'ai une belle erreur au démarrage de mon appliCode:
1
2
3
4
5
6
7
8
9
10 <bean name="propertyPlaceholder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:myFile.properties</value> </list> </property> <property name="localOverride" value="true"/> </bean>
je précise qu'en faisant unCode:
1
2 java.io.FileNotFoundException: class path resource [myFile.properties] cannot be opened because it does not exist
j'ai bien le répertoire où est situé le fichier de configurationCode:
1
2 echo %CLASSPATH%
mais en mettant ma configuration initiale je vois biens que dans les logs il trouve ma propriété mais que visiblement elle n'est pas dans le même contexte que mon bean
et je pense que c'est ça qui pose problème
et je ne sais pas comment les mettre dans le même contexte
ok
Peux-tu donner ces infos :
- Le fichier applicationContext.xml
- Le contenu du fichier properties et l'endroit où il se trouve
- Le contenu du Bean
Voici les fichiers
applicationContext.xml qui se trouve dans le WEB-INF
La fichier my.properties qui se trouve dans le répertoire DIRECTORY_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
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 <?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="com.package" /> <bean name="propertyPlaceholder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>file:///${DIRECTORY_CONFIGURATION}/my.properties</value> </list> </property> <property name="localOverride" value="true"/> </bean> <import resource="classpath:META-INF/cxf/cxf.xml" /> <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> <import resource="classpath:ftp-context.xml" /> <bean id="myClasse" class="com.package.MyClasse" name="MyClasse"> </bean> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <bean id="sqlServerDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" /> <property name="url" value="${conf.db.url}"/> <property name="username" value="${conf.db.username}" /> <property name="password" value="${conf.db.password}" /> </bean> </beans>
Mon beanCode:
1
2 conf.dateformat = MM/dd/yy
et dans mon logger de la méthode getTask j'ai un beau "date format: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
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 package com.package.model; public class MyClasse { private Logger logger = LoggerFactory .getLogger(MyClasse.class); @Value("${conf.dateformat}") private String dateFormat; public String getDateFormat() { return dateFormat; } public void setDateFormat(String _dateFormat) { this.dateFormat = _dateFormat; } public MyClasse() { } public void getObject() { getTask(); } private Object getTask(Map<String, Object> _map) { Object ob = new Object(); logger.debug("date format:" + dateFormat); return ob; } }
Au passage les propriétés ci-dessous sont bien settées
Je ne peux pas mettre le code exactes vu que c'est des classes clientesCode:
1
2 <property name="url" value="${conf.db.url}"/> <property name="username" value="${conf.db.username}" /> <property name="password" value="${conf.db.password}" />
C'est peut-être en me recopiant les fichiers que tu t'es trompé, mais il y a une non correspondance de package entre le bean déclaré dans applicationContext.xml
et la classe du Bean :Code:
1
2
3 <bean id="myClasse" class="com.package.MyClasse" name="MyClasse"> </bean>
mais je suppose que si c'était ça Spring t'enverrai un message d'erreur qqpart...Code:
1
2 package com.package.model;
sorry ..c'est une mauvaise recopie
c'est bien :
Code:
1
2 <bean id="myClasse" class="com.package.model.MyClasse" name="MyClasse"> </bean>
Tout comme tu as l'annotation @Component sur ta classe, je suppose...
Et sinon, as-tu essayé sans le dot, avec dans le properties file :
et dans la classe :Code:
1
2 dateformat = MM/dd/yy
Code:
1
2
3 @Value("${dateformat}") private String dateFormat;
j'ai le component bien sûr
mais je n'ai pas essayé sans le dot
je fais le test et je te redis
pas mieux :aie:
Tout comme tu as bien :
?Code:
1
2 <context:component-scan base-package="com.package.model" />
non j'ai juste
mais même en mettantCode:
1
2 <context:component-scan base-package="com.package" />
ça ne change rienCode:
1
2 <context:component-scan base-package="com.package.model" />
Et comment se fait le chargement du contexte ?
via la servlet :
ou avec un Listener :Code:
1
2
3
4
5
6<servlet> <servlet-name>context</servlet-name> <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet>
Code:
1
2
3
4 <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>