setter方法注入与内部注入

1、
package com.itheima.f_xml.b_setter;

public class Person {
	
	private String pname;
	private Integer age;
	
	private Address homeAddr;		//家庭地址
	private Address companyAddr;	//公司地址
	public String getPname() {
		return pname;
	}
	public void setPname(String pname) {
		this.pname = pname;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public Address getHomeAddr() {
		return homeAddr;
	}
	public void setHomeAddr(Address homeAddr) {
		this.homeAddr = homeAddr;
	}
	public Address getCompanyAddr() {
		return companyAddr;
	}
	public void setCompanyAddr(Address companyAddr) {
		this.companyAddr = companyAddr;
	}
	@Override
	public String toString() {
		return "Person [pname=" + pname + ", age=" + age + ", homeAddr=" + homeAddr + ", companyAddr=" + companyAddr + "]";
	}

	
	
}


2、

package com.itheima.f_xml.b_setter;

public class Address {
	
	private String addr;	//地址信息
	private String tel;		//电话
	
	
	public String getAddr() {
		return addr;
	}
	public void setAddr(String addr) {
		this.addr = addr;
	}
	public String getTel() {
		return tel;
	}
	public void setTel(String tel) {
		this.tel = tel;
	}
	@Override
	public String toString() {
		return "Address [addr=" + addr + ", tel=" + tel + "]";
	}
	
	

}

3、beans.xml



	
	
	
		
		
			1234
		
		
		
		
			
		
	
	
	
		
		
	
	
		
		
	
	

4、测试

package com.itheima.f_xml.b_setter;

import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class TestSetter {
	
	@Test
	public void demo01(){
		//从spring容器获得
		String xmlPath = "com/itheima/f_xml/b_setter/beans.xml";
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
		Person person = (Person) applicationContext.getBean("personId");
		
		System.out.println(person);
		
	}

}


你可能感兴趣的:(javaweb)