1. Using org.springframework.context.MessageSource:
Its using read message in the class files and display message as the output, below given example show how to use properties files with org.springframework.context.MessageSource.
myMessage.properties
greeting=Hello Dinesh! drawing.circle=Circle is Drawn!
spring.xml
<beans xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:>Now "messageSource" using as property of the bean circle as follows:Circle.javapackage com.dineshonjava.sdnext.tutorial.property; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.MessageSource; import org.springframework.stereotype.Component; /** * @author Dinesh Rajput * */ @Component public class Circle { @Autowired private Point center; @Autowired private MessageSource messageSource; /** * @param messageSource the messageSource to set */ public void setMessageSource(MessageSource messageSource) { this.messageSource = messageSource; } /** * @param center the center to set */ public void setCenter(Point center) { this.center = center; } public void draw() { System.out.println(this.messageSource.getMessage("drawing.circle", null, "Default Drawing Greeting", null)); } }
In the above class file two property "center" and "messageSource" which autowired with the bean name "circle". We get the message of the properties file in the draw() method by using following method
org.springframework.context.MessageSource.getMessage(String arg0, Object[] arg1, String arg2, Locale arg3)
where String arg0 give the key of properties file to read its related message.as our example we are using "drawing.circle" and "greeting" as keys.
Object[] arg1 is the array of parameters which are applied to the properties file.Example.
drawing.point=Circle: Point is: ({0}, {1})Here there are two parameters {0}, {1} passes asgetMessage("drawing.point", new Object[]{center.getX(), center.getY()}, "Default Drawing Greeting", null)Here center.getX() is replace with the {0}and center.getY() is replace with the {1}Point.java
package com.dineshonjava.sdnext.tutorial.property; /** * @author Dinesh Rajput * */ public class Point { private int x; private int y; /** * @return the x */ public int getX() { return x; } /** * @param x the x to set */ public void setX(int x) { this.x = x; } /** * @return the y */ public int getY() { return y; } /** * @param y the y to set */ public void setY(int y) { this.y = y; } }
Now run the following class file.DrawingApp.java
package com.dineshonjava.sdnext.tutorial.property; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * @author Dinesh Rajput * */ public class DrawingApp { /** * @param args */ public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); System.out.println(context.getMessage("greeting", null, "Default Greeting", null)); Circle circle = (Circle)context.getBean("circle"); shape.draw(); } }
If every thing ok then we get the following output:Output:Jul 18, 2012 9:04:33 PM org.springframework.context.support.AbstractApplicationContext prepareRefreshINFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@758fc9: startup date [Wed Jul 18 21:04:33 IST 2012]; root of context hierarchyJul 18, 2012 9:04:33 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitionsINFO: Loading XML bean definitions from class path resource [spring.xml]Jul 18, 2012 9:04:34 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletonsINFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1238bd2: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,circle,center,messageSource,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; root of factory hierarchyHello Dinesh!
Circle is Drawn!
Using properties file with parameters:
myMessage.properties
greeting=Hello Dinesh! drawing.circle=Circle is Drawn! drawing.point=Circle: Point is: ({0}, {1})
public void draw() { System.out.println(this.messageSource.getMessage("drawing.circle", null, "Default Drawing Greeting", null)); System.out.println(this.messageSource.getMessage("drawing.point", new Object[] {center.getX(), center.getY()}, "Default Drawing Greeting", null)); }
2. Using org.springframework.beans.factory.config.PropertyPlaceholderConfigurer:
myMessage.properties
X-axis=20 Y-axis=0
spring.xml
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" id="placeholderConfig"> <property name="location" value="classpath:myMessage.properties"> </property> </bean> <bean class="com.dineshonjava.sdnext.tutorial.property.Point" id="center"> <property name="x" value="${X-axis}"></property> <property name="y" value="${Y-axis}"></property> </bean>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…