Skip to content

Commit c1eefd6

Browse files
author
vgerton
committed
add message source support
1 parent 01046e7 commit c1eefd6

File tree

5 files changed

+29
-4
lines changed

5 files changed

+29
-4
lines changed

src/main/java/spring/tutorial/maven/domain/Circle.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,19 @@
44
import org.springframework.beans.factory.annotation.Autowired;
55
import org.springframework.beans.factory.annotation.Qualifier;
66
import org.springframework.beans.factory.annotation.Required;
7+
import org.springframework.context.MessageSource;
78
import org.springframework.stereotype.Component;
89

10+
import java.util.Locale;
11+
912
@Component
1013
public class Circle implements Shape{
1114

1215
@Autowired
1316
@Qualifier("pointA")
1417
private Point center;
18+
@Autowired
19+
private MessageSource messageSource;
1520

1621
public Point getCenter() {
1722
return center;
@@ -25,5 +30,6 @@ public void setCenter(Point center) {
2530
@Override
2631
public void draw() {
2732
System.out.println("Draw circle with the center: "+center);
33+
System.out.println(messageSource.getMessage("greeting", null, "Default greeting!", new Locale("ru")));
2834
}
2935
}

src/main/java/spring/tutorial/maven/domain/Point.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package spring.tutorial.maven.domain;
22

3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.context.MessageSource;
5+
6+
import java.util.Locale;
7+
38
/**
49
* @author Mikalai Kisel
510
*/
@@ -8,6 +13,9 @@ public class Point {
813
private int x;
914
private int y;
1015

16+
@Autowired
17+
private MessageSource messageSource;
18+
1119
public int getX() {
1220
return x;
1321
}
@@ -26,9 +34,6 @@ public void setY(int y) {
2634

2735
@Override
2836
public String toString() {
29-
return "Point{" +
30-
"x=" + x +
31-
", y=" + y +
32-
'}';
37+
return messageSource.getMessage("drawing.point", new Object[] {x, y}, "Default drawing point message!", new Locale("ru"));
3338
}
3439
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
greeting = Hello!
2+
drawing.point = Point ( {0}, {1})
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
greeting = Привет!
2+
drawing.point = Точка ( {0}, {1})

src/main/resources/spring.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@
4545

4646
<!--<annotation-driven/>-->
4747

48+
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
49+
<property name="basenames">
50+
<list>
51+
<value>mymessages</value>
52+
</list>
53+
</property>
54+
55+
</bean>
56+
57+
4858
<context:annotation-config/>
4959

5060
<context:component-scan base-package="spring.tutorial.maven.domain" />

0 commit comments

Comments
 (0)