examples
This commit is contained in:
parent
6cca052251
commit
4b3c52df0f
|
@ -0,0 +1,21 @@
|
|||
package com.baeldung.applicationcontext;
|
||||
|
||||
public class Course {
|
||||
|
||||
private String name;
|
||||
|
||||
public Course() {
|
||||
}
|
||||
|
||||
public Course(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
package com.baeldung.applicationcontext;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
public class Dialog {
|
||||
|
||||
private Locale locale;
|
||||
private String hello;
|
||||
private String thanks;
|
||||
|
||||
public Dialog() {
|
||||
}
|
||||
|
||||
public Dialog(Locale locale, String hello, String thanks) {
|
||||
this.locale = locale;
|
||||
this.hello = hello;
|
||||
this.thanks = thanks;
|
||||
}
|
||||
|
||||
public Locale getLocale() {
|
||||
return locale;
|
||||
}
|
||||
|
||||
public void setLocale(Locale locale) {
|
||||
this.locale = locale;
|
||||
}
|
||||
|
||||
public String getHello() {
|
||||
return hello;
|
||||
}
|
||||
|
||||
public void setHello(String hello) {
|
||||
this.hello = hello;
|
||||
}
|
||||
|
||||
public String getThanks() {
|
||||
return thanks;
|
||||
}
|
||||
|
||||
public void setThanks(String thanks) {
|
||||
this.thanks = thanks;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
package com.baeldung.applicationcontext;
|
||||
|
||||
public class Student {
|
||||
|
||||
private int no;
|
||||
private String name;
|
||||
|
||||
public Student() {
|
||||
}
|
||||
|
||||
public Student(int no, String name) {
|
||||
this.no = no;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getNo() {
|
||||
return no;
|
||||
}
|
||||
|
||||
public void setNo(int no) {
|
||||
this.no = no;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void destroy() {
|
||||
System.out.println("Student(no: " + no + ") is destroyed");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
package com.baeldung.applicationcontext;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
public class Teacher {
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext context;
|
||||
private List<Course> courses = new ArrayList<>();
|
||||
|
||||
public Teacher() {
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void addCourse() {
|
||||
if (context.containsBean("math")) {
|
||||
Course math = context.getBean("math", Course.class);
|
||||
courses.add(math);
|
||||
}
|
||||
if (context.containsBean("physics")) {
|
||||
Course physics = context.getBean("physics", Course.class);
|
||||
courses.add(physics);
|
||||
}
|
||||
}
|
||||
|
||||
public List<Course> getCourses() {
|
||||
return courses;
|
||||
}
|
||||
|
||||
public void setCourses(List<Course> courses) {
|
||||
this.courses = courses;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xmlns="http://www.springframework.org/schema/beans"
|
||||
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">
|
||||
<context:annotation-config/>
|
||||
|
||||
<bean id="student" class="com.baeldung.applicationcontext.Student" destroy-method="destroy">
|
||||
<property name="no" value="15"/>
|
||||
<property name="name" value="Tom"/>
|
||||
</bean>
|
||||
|
||||
<bean id="math" class="com.baeldung.applicationcontext.Course">
|
||||
<property name="name" value="math"/>
|
||||
</bean>
|
||||
|
||||
<bean name="teacher" class="com.baeldung.applicationcontext.Teacher"/>
|
||||
</beans>
|
|
@ -0,0 +1,14 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://www.springframework.org/schema/beans"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||
|
||||
<bean id="messageSource"
|
||||
class="org.springframework.context.support.ResourceBundleMessageSource">
|
||||
<property name="basenames">
|
||||
<list>
|
||||
<value>dialog/dialog</value>
|
||||
</list>
|
||||
</property>
|
||||
</bean>
|
||||
</beans>
|
|
@ -0,0 +1,3 @@
|
|||
hello=hello
|
||||
you=you
|
||||
thanks=thank {0}
|
|
@ -0,0 +1,3 @@
|
|||
hello=\u4f60\u597d
|
||||
you=\u4f60
|
||||
thanks=\u8c22\u8c22{0}
|
|
@ -0,0 +1,59 @@
|
|||
package com.baeldung.applicationcontext;
|
||||
|
||||
import static org.hamcrest.core.IsEqual.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
public class ClasspathXmlApplicationContextTest {
|
||||
@Test
|
||||
public void testBasicUsage() {
|
||||
ApplicationContext context = new ClassPathXmlApplicationContext("classpathxmlapplicationcontext-example.xml");
|
||||
Student student = (Student) context.getBean("student");
|
||||
assertThat(student.getNo(), equalTo(15));
|
||||
assertThat(student.getName(), equalTo("Tom"));
|
||||
|
||||
Student sameStudent = context.getBean("student", Student.class);// do not need cast class
|
||||
assertThat(sameStudent.getNo(), equalTo(15));
|
||||
assertThat(sameStudent.getName(), equalTo("Tom"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRegisterShutdownHook() {
|
||||
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("classpathxmlapplicationcontext-example.xml");
|
||||
context.registerShutdownHook();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInternationalization() {
|
||||
MessageSource resources = new ClassPathXmlApplicationContext("classpathxmlapplicationcontext-internationalization.xml");
|
||||
|
||||
String enHello = resources.getMessage("hello", null, "Default", Locale.ENGLISH);
|
||||
String enYou = resources.getMessage("you", null, Locale.ENGLISH);
|
||||
String enThanks = resources.getMessage("thanks", new Object[] { enYou }, Locale.ENGLISH);
|
||||
assertThat(enHello, equalTo("hello"));
|
||||
assertThat(enThanks, equalTo("thank you"));
|
||||
|
||||
String chHello = resources.getMessage("hello", null, "Default", Locale.SIMPLIFIED_CHINESE);
|
||||
String chYou = resources.getMessage("you", null, Locale.SIMPLIFIED_CHINESE);
|
||||
String chThanks = resources.getMessage("thanks", new Object[] { chYou }, Locale.SIMPLIFIED_CHINESE);
|
||||
assertThat(chHello, equalTo("你好"));
|
||||
assertThat(chThanks, equalTo("谢谢你"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testApplicationContextAware() {
|
||||
ApplicationContext context = new ClassPathXmlApplicationContext("classpathxmlapplicationcontext-example.xml");
|
||||
Teacher teacher = context.getBean("teacher", Teacher.class);
|
||||
List<Course> courses = teacher.getCourses();
|
||||
assertThat(courses.size(), equalTo(1));
|
||||
assertThat(courses.get(0).getName(), equalTo("math"));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue