I'm having problems taking the message values to a properties file.
I'm using intelliJ IDEA and i have a package
com.test.messages
and inside I have the messages.properties file.
Here is my xml
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="com.test.messages.messages"/>
</bean>
When I restart the server this is the WARNING i get
WARNING: ResourceBundle [com.test.messages.messages] not found for MessageSource: Can't find bundle for base name com.test.messages.messages, locale en_US
Any ideas?
Do not keep properties in packages.
Resources directory is a standart place to keep internal properties.
/src/main/resources
or
/src/test/resources
If you want to keep properties separated from the project - use something this kind of spring configuration:
<bean id="props" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
<property name="ignoreResourceNotFound" value="true"/>
<property name="localOverride" value="true"/>
<property name="locations">
<list>
<value>classpath*:messages.properties</value>
<value>file:messages.properties</value>
</list>
</property>
</bean>
So you'd be able to override internal properties with external (don't forget to add the to a classpath using -cp).
Anyway the declaration of the been will look like this:
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="${property.name}"/>
</bean>
you should have a file in your project as such:
src\main\resources\com\test\messages.properties
Although I am not IDEA expert, I know that resources such as .property files placed in the source code hierarchy are copied to the classpath when using the IDE alone. However, if you use maven, then it takes over the build and whatever is in the java path is not copied by default. You have to place all your classpath resources in src\main\resources and under whatever hierarchy mirrors the package name to have the same result. So in your case :
src\main\resources\com\test\messages\messages.properties
and maven will copy it properly
I know this is old question, but let me drop my solution :)
Source folder (resources is not package):
/src/main/java/
somepackage/abc.java
/src/main/resources/
messages.properties
Eclipse default output java build path:
myProject/build/classes
Eclipse default deploy path:
WEB-INF/classes
My project's spring context XML file path:
/src/main/webapp/WEB-INF/context.xml
Inject resource bundles,
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames" value="messages"/>
</bean>
As Azee said keeping properties in package is not encourage. In case, messages.properties is in resources package as below:
/src/resources/messages.properties
context.xml should be like:
<property name="basenames" value="resources/messages"/>
Related
I am currently using the following code which is working correctly:
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<value>/WEB-INF/resources/lang/lang</value>
</list>
</property>
<property name="defaultEncoding" value="UTF-8"/>
</bean>
When I change ReloadableResourceBundleMessageSource to just ResourceBundleMessageSource it stops working.
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="/WEB-INF/resources/lang/lang" />
</bean>
I keep getting exceptions saying "No message found under code 'title' for locale 'en_US'." (title is an example)
After reading similar questions, I have also tried changing the value of basename parameter to any of the following, without success:
WEB-INF/resources/lang/lang
.WEB-INF.resources.lang.lang
WEB-INF.resources.lang.lang
resources.lang.lang
lang.lang
lang
classpath:resources.lang.lang
Making a file called lang_en_US.properties didn't help either.
Apparently, it is looking in the java folder (root of classpath) and not the web folder.
I created a new package called lang in the java folder and moved the lang*.properties files to it.
Then I put lang.lang as the value for basename and it started to work.
UPDATE: This stopped working after a project clean. The lang files should go into the "resources" folder, not the java folder. They technically end up in the same place after a build, but the IDE seems to ignore non-class files in the java folder and simply skips them.
I have a Spring properties file defined as:
<bean
class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="locations">
<list>
<value>lanchecker.properties</value>
</list>
</property>
</bean>
According to 23.3 in http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html:
SpringApplication will load properties from application.properties files in the following locations and add them to the Spring Environment:
A /config subdir of the current directory.
The current directory
A classpath /config package
The classpath root
The list is ordered by precedence (properties defined in locations higher in the list override those defined in lower locations).
During development this file resides on the CLASSPATH, at /src/main/resources, and is resolved fine. After packaging I provide another in the current directory but it is ignored and the one from the CLASSPATH still used.
What have I missed here please?
This appears to work:
<bean
class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<value>classpath:lanchecker.properties</value>
<value>file:${user.dir}/lanchecker.properties</value> <!-- file values will override classpath if exists -->
</list>
</property>
</bean>
If both exist it will read the resource on the classpath first, then replace the values there with anything in the file.
If neither exist they'll just be a nasty traceback at some later stage when the property values come to be used. There are though some INFO log messages to help solve this. No pleasant, but it's the best I've been able to come up with today
I want to have a project independent configuration file that I can access from different projects. What I'm currently trying (and does not give me good results at all):
<bean id="wroProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location" value="${JBOSS_HOME}/standalone/configuration/wro.properties" />
</bean>
I use Spring 3 and JBoss 7.1. My configuration files are under jboss/standalone/configuration/....properties. Besides that I want to read message files from that same directory with:
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames" value="messages,local" />
<property name="useCodeAsDefaultMessage" value="true" />
</bean>
Currently it looks for messages.properties and local.properties in src folder?
This is the solution I ended up using, which is platform independent and portable:
<bean id="wroProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location" value="file:#{systemProperties['jboss.home.dir']}/standalone/configuration/wro.properties" />
</bean>
The configuration of the message source is identical.
A ResourceBundleMessageSource uses the basenames provided to (and the locale) to build a resource name (ex. message.properties) which is eventually (in the call stack) used by java.util.ResourceBundle.Control#newBundle(...). This resource name is then looked for on the classpath starting at its root (ex. /message.properties).
If you're on an IDE like Eclipse, your classpath very likely starts at src.
If jboss/standalone/configuration/... is on your classpath as well and the properties file are in there, you can change the basenames to
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames" value="jboss/standalone/configuration/messages,jboss/standalone/configuration/local" />
<property name="useCodeAsDefaultMessage" value="true" />
</bean>
I'm building my project with maven so according to maven way, config should be in src/main/conf , how can I say to my spring application context that that is where jdbc.properties is found? Here is example bean :
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="jdbc.properties" />
</bean>
Spring assumens that this configuration is inside src/main/webapp/WEB-INF, I hope I've been clear if not I'll rephrase my question thank you
I'm building my project with maven so
according to maven way, config should
be in src/main/conf
Actually, configuration data should generally go in src/main/resources, that way it will be on the classpath and you can reference your property file like:
<property name="location" value="classpath:jdbc.properties" />
I think it is not quite clear as to what a "config file" means. I am thinking it is the config files used by the other maven plugins (such as surefire plugins, assembly plugins etc).
Surely in the 10+ web app projects that I have worked the bean files, jdbc.properties files have all been under src/main/resources
Try this
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="src/main/config/jdbc.properties" />
</bean>
Is there a way to reference a properties file on a file system (not on a classpath) relative to the Spring's context file itself?
What I want to do is the below:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<!-- I want the below to be relative to this context XML file. -->
<value>app.properties</value>
</list>
</property>
</bean>
I was imagining something like ${contextpath} that I could prepend to the above "app.properties" but couldn't find anything helpful.
Thanks.
You can reference properties on the classpath via the classpath: prefix:
<value>classpath:app.properties</value>