This commit is contained in:
DianeDuan
2016-12-11 18:27:47 +08:00
parent 6cca052251
commit 4b3c52df0f
9 changed files with 235 additions and 0 deletions
@@ -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}