Spring injection member object
Is to inject the object into another object. In this example, a school class has a principal class, and the test file is used to output information in the school class.
Code structure
School
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHByZSBjbGFzcz0 = "brush: java;"> package com. test. springGetSet; public class School {private Present present; public Present getPresent () {return present;} public void setPresent (Present present) {this. present = present;} public String getShoolName () {return shoolName;} public void setShoolName (String shoolName) {this. shoolName = shoolName;} String shoolName; public void say () {System. out. println ("school name:" + shoolName); System. out. println ("Principal name:" + present. getName ());}}
Principal
package com.test.SpringGetSet;public class Present {private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}private int age;}
Beans. xml
Test class
package com.test.SpringGetSet;import org.springframework.beans.factory.BeanFactory;import org.springframework.beans.factory.xml.XmlBeanFactory;import org.springframework.core.io.ClassPathResource;import SpringByHand.Hello;public class SetDemo001 {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubBeanFactory factory=new XmlBeanFactory(new ClassPathResource("com/test/SpringGetSet/beans.xml"));School s=(School) factory.getBean("school");s.say();}}
Output result