Spring injection member object

Source: Internet
Author: User

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



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.