Skip to content

Commit 4b3c52d

Browse files
author
DianeDuan
committed
examples
1 parent 6cca052 commit 4b3c52d

9 files changed

Lines changed: 235 additions & 0 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.applicationcontext;
2+
3+
public class Course {
4+
5+
private String name;
6+
7+
public Course() {
8+
}
9+
10+
public Course(String name) {
11+
this.name = name;
12+
}
13+
14+
public String getName() {
15+
return name;
16+
}
17+
18+
public void setName(String name) {
19+
this.name = name;
20+
}
21+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung.applicationcontext;
2+
3+
import java.util.Locale;
4+
5+
public class Dialog {
6+
7+
private Locale locale;
8+
private String hello;
9+
private String thanks;
10+
11+
public Dialog() {
12+
}
13+
14+
public Dialog(Locale locale, String hello, String thanks) {
15+
this.locale = locale;
16+
this.hello = hello;
17+
this.thanks = thanks;
18+
}
19+
20+
public Locale getLocale() {
21+
return locale;
22+
}
23+
24+
public void setLocale(Locale locale) {
25+
this.locale = locale;
26+
}
27+
28+
public String getHello() {
29+
return hello;
30+
}
31+
32+
public void setHello(String hello) {
33+
this.hello = hello;
34+
}
35+
36+
public String getThanks() {
37+
return thanks;
38+
}
39+
40+
public void setThanks(String thanks) {
41+
this.thanks = thanks;
42+
}
43+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeldung.applicationcontext;
2+
3+
public class Student {
4+
5+
private int no;
6+
private String name;
7+
8+
public Student() {
9+
}
10+
11+
public Student(int no, String name) {
12+
this.no = no;
13+
this.name = name;
14+
}
15+
16+
public int getNo() {
17+
return no;
18+
}
19+
20+
public void setNo(int no) {
21+
this.no = no;
22+
}
23+
24+
public String getName() {
25+
return name;
26+
}
27+
28+
public void setName(String name) {
29+
this.name = name;
30+
}
31+
32+
public void destroy() {
33+
System.out.println("Student(no: " + no + ") is destroyed");
34+
}
35+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.baeldung.applicationcontext;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import javax.annotation.PostConstruct;
7+
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.context.ApplicationContext;
10+
11+
public class Teacher {
12+
13+
@Autowired
14+
private ApplicationContext context;
15+
private List<Course> courses = new ArrayList<>();
16+
17+
public Teacher() {
18+
}
19+
20+
@PostConstruct
21+
public void addCourse() {
22+
if (context.containsBean("math")) {
23+
Course math = context.getBean("math", Course.class);
24+
courses.add(math);
25+
}
26+
if (context.containsBean("physics")) {
27+
Course physics = context.getBean("physics", Course.class);
28+
courses.add(physics);
29+
}
30+
}
31+
32+
public List<Course> getCourses() {
33+
return courses;
34+
}
35+
36+
public void setCourses(List<Course> courses) {
37+
this.courses = courses;
38+
}
39+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns:context="http://www.springframework.org/schema/context"
4+
xmlns="http://www.springframework.org/schema/beans"
5+
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">
6+
<context:annotation-config/>
7+
8+
<bean id="student" class="com.baeldung.applicationcontext.Student" destroy-method="destroy">
9+
<property name="no" value="15"/>
10+
<property name="name" value="Tom"/>
11+
</bean>
12+
13+
<bean id="math" class="com.baeldung.applicationcontext.Course">
14+
<property name="name" value="math"/>
15+
</bean>
16+
17+
<bean name="teacher" class="com.baeldung.applicationcontext.Teacher"/>
18+
</beans>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://www.springframework.org/schema/beans"
4+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
5+
6+
<bean id="messageSource"
7+
class="org.springframework.context.support.ResourceBundleMessageSource">
8+
<property name="basenames">
9+
<list>
10+
<value>dialog/dialog</value>
11+
</list>
12+
</property>
13+
</bean>
14+
</beans>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
hello=hello
2+
you=you
3+
thanks=thank {0}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
hello=\u4f60\u597d
2+
you=\u4f60
3+
thanks=\u8c22\u8c22{0}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.baeldung.applicationcontext;
2+
3+
import static org.hamcrest.core.IsEqual.equalTo;
4+
import static org.junit.Assert.assertThat;
5+
6+
import java.util.List;
7+
import java.util.Locale;
8+
9+
import org.junit.Test;
10+
import org.springframework.context.ApplicationContext;
11+
import org.springframework.context.ConfigurableApplicationContext;
12+
import org.springframework.context.MessageSource;
13+
import org.springframework.context.support.ClassPathXmlApplicationContext;
14+
15+
public class ClasspathXmlApplicationContextTest {
16+
@Test
17+
public void testBasicUsage() {
18+
ApplicationContext context = new ClassPathXmlApplicationContext("classpathxmlapplicationcontext-example.xml");
19+
Student student = (Student) context.getBean("student");
20+
assertThat(student.getNo(), equalTo(15));
21+
assertThat(student.getName(), equalTo("Tom"));
22+
23+
Student sameStudent = context.getBean("student", Student.class);// do not need cast class
24+
assertThat(sameStudent.getNo(), equalTo(15));
25+
assertThat(sameStudent.getName(), equalTo("Tom"));
26+
}
27+
28+
@Test
29+
public void testRegisterShutdownHook() {
30+
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("classpathxmlapplicationcontext-example.xml");
31+
context.registerShutdownHook();
32+
}
33+
34+
@Test
35+
public void testInternationalization() {
36+
MessageSource resources = new ClassPathXmlApplicationContext("classpathxmlapplicationcontext-internationalization.xml");
37+
38+
String enHello = resources.getMessage("hello", null, "Default", Locale.ENGLISH);
39+
String enYou = resources.getMessage("you", null, Locale.ENGLISH);
40+
String enThanks = resources.getMessage("thanks", new Object[] { enYou }, Locale.ENGLISH);
41+
assertThat(enHello, equalTo("hello"));
42+
assertThat(enThanks, equalTo("thank you"));
43+
44+
String chHello = resources.getMessage("hello", null, "Default", Locale.SIMPLIFIED_CHINESE);
45+
String chYou = resources.getMessage("you", null, Locale.SIMPLIFIED_CHINESE);
46+
String chThanks = resources.getMessage("thanks", new Object[] { chYou }, Locale.SIMPLIFIED_CHINESE);
47+
assertThat(chHello, equalTo("你好"));
48+
assertThat(chThanks, equalTo("谢谢你"));
49+
}
50+
51+
@Test
52+
public void testApplicationContextAware() {
53+
ApplicationContext context = new ClassPathXmlApplicationContext("classpathxmlapplicationcontext-example.xml");
54+
Teacher teacher = context.getBean("teacher", Teacher.class);
55+
List<Course> courses = teacher.getCourses();
56+
assertThat(courses.size(), equalTo(1));
57+
assertThat(courses.get(0).getName(), equalTo("math"));
58+
}
59+
}

0 commit comments

Comments
 (0)