【尚硅谷】spring学习笔记(3):引用其它 Bean、内部 Bean、集合属性

引用其它 Bean

  • 组成应用程序的 Bean 经常需要相互协作以完成应用程序的功能. 要使 Bean 能够相互访问, 就必须在 Bean 配置文件中指定对 Bean 的引用
  • 在 Bean 的配置文件中, 可以通过 <ref> 元素或 ref  属性为 Bean 的属性或构造器参数指定对 Bean 的引用. 
  • 也可以在属性或构造器里包含 Bean 的声明, 这样的 Bean 称为内部 Bean

内部 Bean

  • 当 Bean 实例仅仅给一个特定的属性使用时, 可以将其声明为内部 Bean. 内部 Bean 声明直接包含在 <property> 或 <constructor-arg> 元素里, 不需要设置任何 id 或 name 属性
  • 内部 Bean 不能使用在任何其他地方
  • 可以使用专用的 <null/> 元素标签为 Bean 的字符串或其它对象类型的属性注入 null 值
  • 和 Struts、Hiberante 等框架一样,Spring 支持级联属性的配置。

集合属性

  • 在 Spring中可以通过一组内置的 xml 标签(例如: <list>, <set> 或 <map>) 来配置集合属性.
  • 配置 java.util.List 类型的属性, 需要指定 <list>  标签, 在标签里包含一些元素. 这些标签可以通过 <value> 指定简单的常量值, 通过 <ref> 指定对其他 Bean 的引用. 通过<bean> 指定内置 Bean 定义. 通过 <null/> 指定空元素. 甚至可以内嵌其他集合.
  • 数组的定义和 List 一样, 都使用 <list>
  • 配置 java.util.Set 需要使用 <set> 标签, 定义元素的方法与 List 一样.
  • Java.util.Map 通过 <map> 标签定义, <map> 标签里可以使用多个 <entry> 作为子标签. 每个条目包含一个键和一个值. 
  • 必须在 <key> 标签里定义键
  • 因为键和值的类型没有限制, 所以可以自由地为它们指定 <value>, <ref>, <bean> 或 <null> 元素. 
  • 可以将 Map 的键和值作为 <entry> 的属性定义: 简单常量使用 key 和 value 来定义; Bean 引用通过 key-ref 和 value-ref 属性定义
  • 使用 <props> 定义 java.util.Properties, 该标签使用多个 <prop> 作为子标签. 每个 <prop> 标签必须定义 key 属性.

参考配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
	
	<!-- 属性注入配置bean -->
	<bean id="helloWorld" class="com.atguigu.spring.helloworld.HelloWorld2">
	    <property name="name" value="ligang"></property>
	</bean>
	
	<!-- 若一个 bean 有多个构造器, 如何通过构造器来为 bean 的属性赋值 -->
	<!-- 可以根据 index 和 value 进行更加精确的定位. -->
	<bean id ="bus" class="com.atguigu.spring.helloworld.Bus" >
	    <constructor-arg value="KUGA" index="1"></constructor-arg>
	    <constructor-arg value="ChangAnFord" index="0"></constructor-arg>
		<constructor-arg value="250000" type="int"></constructor-arg>
	</bean>
	
	<bean id ="bus2" class="com.atguigu.spring.helloworld.Bus" >
	    <constructor-arg value="小明" index="1"></constructor-arg>
	    <constructor-arg value="Chang" index="0"></constructor-arg>
		<constructor-arg value="400000" type="int"></constructor-arg>
	</bean>    
	
	
	<bean id="person2" class="com.atguigu.spring.helloworld.Person2">
	     <property name="name" value="小明"></property>
	     <property name="age" value="26"></property>
	 <!--<property name="buss">
	           <list>
	               <ref bean="bus"/>
	               <ref bean ="bus2"/>
	           </list>
	     </property>-->
	     <property name="buss" ref="buss"></property>
	     
	     <property name="busss">
	           <map>
	              <entry key="第一个" value-ref="bus"></entry>
	              <entry key="第二个" value-ref="bus2"></entry>
	           </map>
	     </property>
	</bean>
	
	
	<util:list id="buss">
	    <ref bean="bus"/>
	    <ref bean="bus2"/>
	</util:list>
	<!-- 配置properties属性值 -->
	<bean id="dataSource" class="com.atguigu.spring.helloworld.DataSource">
	     <property name="properties">
	          <props>
	              <prop key="user">root</prop>
	              <prop key="password">1234</prop>
	              <prop key="jdbcUrl">jdbc:mysql:///test</prop>
	          </props>
	     </property>
	</bean>
	
	
</beans>
public class Bus {
	private String name;
	private String brand;
	private double price;
	private int maxSpeed;
	
	public Bus(String name, String brand, int maxSpeed) {
		super();
		this.name = name;
		this.brand = brand;
		this.maxSpeed = maxSpeed;
	}

	public Bus(String name, String brand, double price) {
		super();
		this.name = name;
		this.brand = brand;
		this.price = price;
	}

	@Override
	public String toString() {
		return "myBus [name=" + name + ", brand=" + brand + ", price=" + price + ", maxSpeed=" + maxSpeed + "]";
	}

	public void setPrice(double price) {
		this.price = price;
	}
}
import java.util.Properties;

public class DataSource {
	
	private Properties properties;

	public Properties getProperties() {
		return properties;
	}

	public void setProperties(Properties properties) {
		this.properties = properties;
	}

	@Override
	public String toString() {
		return "DataSource [properties=" + properties + "]";
	}

}
public class HelloWorld {

	private String user;
	
	public HelloWorld() {
		System.out.println("HelloWorld's constructor...");
	}
	
	public void setUser(String user) {
		System.out.println("setUser:" + user);
		this.user = user;
	}
	
	public HelloWorld(String user) {
		this.user = user;
	}

	public void hello(){
		System.out.println("Hello: " + user);
	}
	
}
public class Main {
	
	public static void main(String[] args) {
		

		
		//1. 创建 Spring 的 IOC 容器
		ClassPathXmlApplicationContext cpa = new ClassPathXmlApplicationContext("applicationContext2.xml");
		
		//2. 从 IOC 容器中获取 bean 的实例
		HelloWorld2 helloWorld = (HelloWorld2) cpa.getBean("helloWorld");
		
		//3. 使用 bean
		helloWorld.hello();
	
		Bus bus = (Bus) cpa.getBean("bus");
		System.out.println(bus.toString());
		
		Bus bus2 = (Bus) cpa.getBean("bus2");
		System.out.println(bus2.toString());
		
		Person2 person = (Person2) cpa.getBean("person2");
		System.out.println(person);
		
		DataSource dataSource = (DataSource) cpa.getBean("dataSource");
		System.out.println(dataSource.getProperties());
			
	}
	
}
import java.util.List;
import java.util.Map;

public class Person2 {
	private String name;
	private String age;
	
	private List<Bus> buss;
	
	private Map<String, Bus> busss;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAge() {
		return age;
	}

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

	public List<Bus> getBuss() {
		return buss;
	}


	public void setBuss(List<Bus> buss) {
		this.buss = buss;
	}

	public Map<String, Bus> getBusss() {
		return busss;
	}

	public void setBusss(Map<String, Bus> busss) {
		this.busss = busss;
	}

	@Override
	public String toString() {
		return "Person2 [name=" + name + ", age=" + age + ", buss=" + buss + ", busss=" + busss + "]";
	}	
}

结果:

HelloWorld's 构造器...
您好:ligang
myBus [name=ChangAnFord, brand=KUGA, price=0.0, maxSpeed=250000]
myBus [name=Chang, brand=小明, price=0.0, maxSpeed=400000]
Person2 [name=小明, age=26, buss=[myBus [name=ChangAnFord, brand=KUGA, price=0.0, maxSpeed=250000], myBus [name=Chang, brand=小明, price=0.0, maxSpeed=400000]], busss={第一个=myBus [name=ChangAnFord, brand=KUGA, price=0.0, maxSpeed=250000], 第二个=myBus [name=Chang, brand=小明, price=0.0, maxSpeed=400000]}]
{user=root, password=1234, jdbcUrl=jdbc:mysql:///test}

补充:

使用 p 命名空间

  • 为了简化 XML 文件的配置,越来越多的 XML 文件采用属性而非子元素配置信息。
  • Spring 从 2.5 版本开始引入了一个新的 p 命名空间,可以通过 <bean> 元素属性的方式配置 Bean 的属性。
  • 使用 p 命名空间后,基于 XML 的配置方式将进一步简化
<bean id="user3" class="com.atguigu.spring.helloworld.User" p:cars-ref="cars" p:userName="Titannic"></bean>


猜你喜欢

转载自blog.csdn.net/oqkdws/article/details/80612087