Spring injects member objects

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.