spring学习笔记(六)

 spring 自动装配 可以简化配置熟
 两种都在beans标签中配置
 1:根据name(id) 来查询bean  default-autowire="byName"  不用配置类中的类属性,会根据id去查找,
 但是类的对象必须跟id名称保持一致.多用于快速开发,因为看不到继承关系.
 2:根据type(class)来查询bean default-autowire="byType" 不用配置类中的雷属性,会根据类的类型去找
 类的id可以改变

 

结构

 

 

package zhc.love.dj.test;

public class Bean2 {
	private Bean3 bean3 ;
	private Bean4 bean4 ;
	private Bean5 bean5 ;
	
	public Bean3 getBean3() {
		return bean3;
	}
	public void setBean3(Bean3 bean3) {
		this.bean3 = bean3;
	}
	public Bean4 getBean4() {
		return bean4;
	}
	public void setBean4(Bean4 bean4) {
		this.bean4 = bean4;
	}
	public Bean5 getBean5() {
		return bean5;
	}
	public void setBean5(Bean5 bean5) {
		this.bean5 = bean5;
	}
	
}

 

 

  

package zhc.love.dj.test;

public class Bean3 {

	private int id ;
	private String name ;
	private String password ;
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	
}

 

  

  

package zhc.love.dj.test;

public class Bean4 {

	private int id ;
	private String name ;
	
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}

}

 

   

package zhc.love.dj.test;

public class Bean5 {

	private int age ;

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
	
	
	
}

 

 

 

package zhc.love.dj.test;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;



public class TestCase extends junit.framework.TestCase {
	
	 public void testSpringAbstract(){
		 BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext*.xml");
			Bean2 bean2 = (Bean2)factory.getBean("bean2");
     		System.out.println("Bean2.Bean3.id="+ bean2.getBean3().getId());
     		System.out.println("Bean2.Bean3.name="+ bean2.getBean3().getName());
     		System.out.println("Bean2.Bean3.password="+ bean2.getBean3().getPassword());
	 
     		System.out.println("Bean2.Bean4.id="+ bean2.getBean4().getId());
     		System.out.println("Bean2.Bean4.name="+ bean2.getBean4().getName());
     		
     		System.out.println("Bean2.Bean5.age="+ bean2.getBean5().getAge());
	 }
	 

}

 

 

  

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"
	default-autowire="byType">
	<bean id="abstractBean" abstract="true">
		<property name="id" value="111"></property>
		<property name="name" value="chao"></property>
	</bean>

	<bean id="bean13"  class="zhc.love.dj.test.Bean3" parent="abstractBean">
		<property name="password" value="love"></property>
	</bean>

	<bean id="bean4"  class="zhc.love.dj.test.Bean4" parent="abstractBean">
	</bean>
</beans>

 

   在配置中不用配置bean2中的其它属性.减少配置

 

  

<?xml version="1.0" encoding="UTF-8"?>
<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"
	default-autowire="byType" >
<bean id="bean2" class="zhc.love.dj.test.Bean2" />

<bean id="bean5" class="zhc.love.dj.test.Bean5">
<property name="age" value="10"></property>
</bean>

</beans>

 

 

你可能感兴趣的:(spring,bean,xml,JUnit)