zoukankan      html  css  js  c++  java
  • spring注入成员对象

    就是将对象注入到另外一个对象中。这个样例就是有一个学校类,学校类中有一个校长类,最后使用測试文件输出学校类中的信息。

    代码结构



    学校类

    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("校名:"+shoolName);
    		System.out.println("校长姓名:"+present.getName());
    	}
    
    }
    

    校长类

    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

    <?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="杨发癫" /> <property name="age" value="40" /> </bean> <bean id="school" class="com.test.SpringGetSet.School"> <property name="shoolName" value="上饶县中" /> <property name="present"> <ref bean="present"/> </property> </bean> </beans>


    測试类

    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 stub
    		BeanFactory factory=new XmlBeanFactory(new ClassPathResource("com/test/SpringGetSet/beans.xml"));
    		School s=(School) factory.getBean("school");
    		s.say();
    	}
    
    }
    


    输出结果



  • 相关阅读:
    路由器配置深入浅出—静态路由和缺省路由配置
    盘点飞思卡尔i.MX多媒体处理器前世今生 (转)
    ubuntu18.04下取消中键复制粘贴功能
    uboot常用命令及其使用
    MCU软件最佳实践——使用printf打印数据
    uboot无法通过nfs加载ubuntu18.04中的文件(转)
    GNU C字节对齐__attribute__((aligned(n))) #pragma pack(n)
    ENDIAN的由来及BIGEDIAN 和LITTLEENDIAN(转)
    自动生成c# Model属性
    使用JAVA生成随机的AES密钥
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/5280173.html
Copyright © 2011-2022 走看看