上一篇文章里我们学习了注入TeachingService的方法,在这篇文章中将会看一下如何进行普通的值得注入。
定义注入对象的get/set方法
向Student类里面加入name和country两个字段并生成get/set方法(Eclipse右键菜单/IntelliJ的Alt+Insert均可自动生成)
package com.liumiao.demo.spring;
public class Student implements Person {
private String name;
private String country;
public String getName() {
return name;
}
public void setName(String name) {
System.out.println("Student: set method: SetName:"+name);
this.name = name;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
System.out.println("Student: set method: SetCountry:"+country);
this.country = country;
}
private TeachingService teachingService;
public Student(){
System.out.println("Student Default construct is called...");
}
public void setTeachingService(TeachingService service){
teachingService=service;
}
@Override
public String sayhello(){
return "Hello, I am a student.";
}
@Override
public String provideTeachingService(){
return "I teach how to study...";
}
}
配置spring设定文件
如下修改配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="thePerson" class="com.liumiao.demo.spring.Student">
<property name="teachingService" ref="theService"></property>
<property name="name" value="liumiao" />
<property name="country" value="China" />
</bean>
<bean id="theService" class="com.liumiao.demo.spring.SwimmingTeachingService">
</bean>
</beans>
修改TestDemo
package com.liumiao.demo.spring;
import com.liumiao.demo.spring.Person;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestDemo {
public TestDemo() {
}
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:com/liumiao/demo/spring/spring-cfg.xml");
Student person = (Student) context.getBean("thePerson", Person.class);
System.out.println(person.sayhello());
System.out.println(person.provideTeachingService());
System.out.println(person.getCountry());
System.out.println(person.getName());
context.close();
}
}
执行结果
TestDemo无需修改可以直接确认结果。
十一月 26, 2016 9:38:26 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@25f38edc: startup date [Sat Nov 26 21:38:26 CST 2016]; root of context hierarchy
十一月 26, 2016 9:38:26 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [com/liumiao/demo/spring/spring-cfg.xml]
Student Default construct is called...
Student: set method: SetName:liumiao
Student: set method: SetCountry:China
Hello, I am a student.
十一月 26, 2016 9:38:26 下午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
I teach how to study...
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@25f38edc: startup date [Sat Nov 26 21:38:26 CST 2016]; root of context hierarchy
China
liumiao