I want to externalize application message to properties file. I'm loading the properties file using Spring.
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:applicationmessage.properties</value>
</list>
</property>
<property name="ignoreResourceNotFound" value="true" />
I have the message with para
message.myMessage = Couldn't find resource for customer id {0} and business unit {1}
What's the best way to read this message with parameter from java file ? Is there any other approach to externalize the messages.
It depends, exists differents ways to do, directly in jsp, in form validation process, etc..
For example
Message in properties:
msg=My message {0} and {1}.
In your jsp:
<spring:message code="msg"
arguments="${value1},${value2}"
htmlEscape="false"/>
http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/spring.tld.html#spring.tld.message
hi there are several ways to get properties message from spring.
way 1:
<util:properties id="Properties" location="classpath:config/taobaoConfig.properties" />
add this in spring.xml
in your java file . you create following property.
#Resource(name = "Properties")
private Properties serverProperties;
the key-value in properties file will in serverProperties property.
way 2:
create a properties container bean
<bean id="propertyUtil" class="com.PropertiesUtil">
<property name="locations">
<list>
<value>/WEB-INF/classes/datasource.properties</value>
<value>/WEB-INF/classes/fileDef.properties</value>
</list>
</property>
</bean>
code of com.PropertiesUtil
public class PropertiesUtil extends PropertyPlaceholderConfigurer {
private Properties properties;
#Override
protected void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props) {
super.processProperties(beanFactory, props);
this.properties = props;
}
/**
* Get property from properties file.
* #param name property name
* #return property value
*/
public String getProperty(final String name) {
return properties.getProperty(name);
}
}
you can use this container bean to get key-value in properties files .
Related
I have a bean Parent with just one property attr.
class Parent
{
String attr;
public void doStuff(){
//stuff
}
public String getAttr() {
return attr;
}
public void setAttr(String attr) {
this.attr=attr;
}
}
I have three beans that extend this Parent bean.
My spring.xml looks like this-
<bean id="parent" class="Parent"/>
<bean id="child1" parent="parent">
<property name="attr" value="Sample value 1"/>
</bean>
<bean id="child2" parent="parent">
<property name="attr" value="Sample value 2"/>
</bean>
<bean id="child3" parent="parent">
<property name="attr" value="Sample value 3"/>
</bean>
I want to do the same thing using annotations.
Problem is that I have to do this storefront and all the beans in my controller are declared as -
#Resource
#Qualifier("child1")
Parent child1;
Is there a way I can add the property to the child beans using annotations or any other approach in controller i.e. without using spring.xml?
Is there a way of doing this using the #Value annotation. Problem is that I don't have a static value that comes from a property file. I have 3 different value for 3 different beans.
Shouldn’t this work, if you have three different values in your properties file
see post here
#Value(“${child1.attr}”)
#Value(“${child2.attr}”)
#Value(“${child3.attr}”)
Can I define two property files using ResourceBundleMessageSource like:
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>com/app/view/web/AppResource1</value>
<value>com/app/view/web/AppResource2</value>
</list>
</property>
</bean>
If that is possible with ResourceBundleMessageSource, How to use both the property files in Bean file. Till now, I am using only one property file in any bean by injecting messageSource in to it and using like:
public class BeanOne {
public BeanOne(ResourceBundleMessageSource bundleMessageSource) {
this.messageSource = bundleMessageSource;
}
....
this.messageSource.getMessage("",locale);
}
Please tell me how to access both property files in a bean. Thanks.
The properties from both files are included in the message source.
If you have com/app/view/web/AppResource1:
com.app.view.web.propertyA=foo
And com/app/view/web/AppResource2:
com.app.view.web.propertyB=bar
Then in your bean, you access can them:
messageSource.getMessage("com.app.view.web.propertyA", LOCALE); // foo
messageSource.getMessage("com.app.view.web.propertyB", LOCALE); // bar
I have implemented a bean with hibernate and hibernate validation. This is the field I am testing on.
#NotBlank(message ="test.test" )
private String test;
I have a file
messages.properties
and
messages_en.properties
that I use. Spring.message tags work so the files can be found and are used in the system elsewhere. I get "test.test" as my validation error when I try to store an object with that field empty and not error message in messages.properties. What am I missing?
You should try like this by enclosing key name with curly braces. This syntax is different than Java from the localization files. It's followed with Spring/Hibernate Validation thru Validation API.
#NotBlank(message="{test.test}")
private String test;
The problem was that i was missing this in my configuration. Also message="{test.test}" was necessary.
<mvc:annotation-driven validator="validator" />
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="messageInterpolator">
<bean class="org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator">
<constructor-arg index="0">
<bean
class="org.springframework.validation.beanvalidation.MessageSourceResourceBundleLocator">
<constructor-arg index="0" ref="messageSource" />
</bean>
</constructor-arg>
</bean>
</property>
</bean>
Only below worked for me:
Create ValidationMessages.properties in "resources" directory of maven project with the below message property:
app.validation.notempty.msg=must be populated
Then, in dto class:
#NotEmpty(message = "{app.validation.notempty.msg}")
private String fieldName;
Fially validate as shown below. Please note I found provider HibernateValidator.class is mandatory to take the values from .properties file.
ValidatorFactory factory = Validation.byProvider(
HibernateValidator.class )
.configure()
.buildValidatorFactory();
Validator validator = factory.getValidator();
Hope this helps someone.
I'm using glassfish 3.1, spring 3.2 and jdk 1.7.
I have configured two custom JNDI resources in Glassfish. One is called 'config' and the other is called 'mappings'. But when I reference one of them in the code, it actually has the properties for both and all system properties (catalina.base etc). I only want the one, not all 3 sets.
I have it set so I get the properties in the spring context file:
<jee:jndi-lookup id="mappingsJndi" jndi-name="mappings" resource-ref="true" />
<bean id="propertyMappings" class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="propertiesArray">
<list>
<ref bean="mappingsJndi"/>
</list>
</property>
</bean>
I reference it in the servlet. It's injected like this:
#Autowired
Properties[] propertyMappings;
The injection works, but it contains 3 properties objects instead of the one. Is there any way around this?
Looks like I figured it out. Instead of referencing the propertyMappings bean like this:
#Autowired
Properties[] propertyMappings;
I just reference the JNDI lookup directly:
#Autowired
Properties mappingsJndi;
First: I'm using Spring 3.0
I have a problem when configuring my controller class. The controller uses a web service which I want to define the endpoint address using a .properties file.
#Controller
public class SupportController {
#Value("#{url.webservice}")
private String wsEndpoint;
...
In my application context xml-file, I've defined this:
<context:property-placeholder location="/WEB-INF/*.properties" />
I've been reading the documentation, trying different approaches (like adding prefix systemProperties.),but I keep getting an error message telling me that it doesn't exist.
Field or property 'url' cannot be
found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext'
Ok. I've figured it out.
Now, in the controller:
#Value("#{settings['url.webservice']")
Then in the context configuration I have this "helper bean":
<util:properties id="settings"
location="/WEB-INF/supportweb.properties"></util:properties>
This should work, too:
#Value("${url.webservice}")
private String wsEndpoint;
I have this configuration and it works fine:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:application.properties</value>
</list>
</property>
</bean>
and I iniejct the property in this way
#Value("${root.log.level}")
private String prop;
the field is correctly initialized to "DEBUG" value.
you should check that the
<context:property-placeholder location="/WEB-INF/*.properties" />
is defined in webmvc-config.xml where you create instances of the #Controllers