is to inject the object into another object. The example is that there is a school class in which there is a headmaster class, and finally the information in the school class is exported using the test file.
Code structure
School Class
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 class
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.) {this.age = age;} private int age;}
Beans.xml
<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE beans Public "-//spring//dtd bean//en" "Http://www.springframework.org/dtd/spring-beans.dtd" ><beans > <bean id= "Present" class= "Com.test.SpringGetSet.Present" > <property name= "name" value= "Rosie Epilepsy"/ > <property name= "age" value= "all"/> </bean> <bean id= "School" class= " Com.test.SpringGetSet.School "> <property name=" Shoolname "value=" Shangrao County "/> <property name=" Present "> <ref bean=" Present "/> </property> </bean></beans>
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 Aut O-generated method Stubbeanfactory factory=new xmlbeanfactory (New Classpathresource ("com/test/springgetset/ Beans.xml ")); School s= (School) Factory.getbean ("School"); S.say ();}}
Output results
Spring injects member objects