Spring—属性配置细节2

这次我们接着上次讲的集合属性开始:

1. 配置Map属性

  • 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属性

示例:

首先定义一个新类:NewPerson

package com.spring.beans.collection;
import com.spring.beans.*;
import java.util.List;
import java.util.Map;

public class NewPerson {
	
	private String name;
	private int age;
	private Map<String, Car> cars;
	
	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;
	}
	
	
	public Map<String, Car> getCars() {
		return cars;
	}
	public void setCars(Map<String, Car> cars) {
		this.cars = cars;
	}
	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + ", cars=" + cars + "]";
	}
	
	
	public NewPerson(String name, int age, Map<String, Car> cars) {
		super();
		this.name = name;
		this.age = age;
		this.cars = cars;
	}
	public NewPerson() {
		
	}
	
	
	
	
}

配置Map属性:

	<!-- 通过构造方法来配置bean的属性 -->
	<bean id="car" class="com.spring.beans.Car">
	    <constructor-arg value="Audi" index="0"></constructor-arg>
	    <!-- 若字面值中包含特殊字符,可以使用<![CDATA[]]>把字面值包裹起来 -->
	    <constructor-arg index="1">
            <value><![CDATA[<Shanghai>]]></value>
        </constructor-arg>
        <!-- 属性值也可以使用value直接点进行配置 -->
	    <constructor-arg type="int">
	    	<value>3000</value>
	    </constructor-arg>
	</bean>
	<!-- 使用构造器注入属性值可以指定参数的位置和参数的类型!以区分重载的构造器 -->
	<bean id="car2" class="com.spring.beans.Car">
	    <constructor-arg value="Audi" type="java.lang.String"></constructor-arg>
	    <constructor-arg value="AShanghai" type="java.lang.String"></constructor-arg>
	    <constructor-arg value="200" type="double"></constructor-arg>
	</bean>

	<!-- 配置Map属性值 -->
	<bean id = "newperson" class = "com.spring.beans.collection.NewPerson">
		<property name = "name" value = "Rose"></property>
		<property name = "age" value = "19"></property>
		<property name = "cars">
            <!-- 使用map节点及map的entry子节点配置Map类型的成员变量 -->
			<map>
				<entry key = "AA" value-ref = "car"></entry>
				<entry key = "BB" value-ref = "car2"></entry>
			</map>
		</property>
	</bean>

Main中使用:

package com.spring.beans.collection;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
	public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		
		NewPerson newPerson = (NewPerson) ctx.getBean("newperson");
		System.out.println(newPerson);
	}
}

1.2 配置properties属性:

新建DataSource类:

package com.spring.beans.collection;

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 + "]";
	}
	
	
}

配置properties

	<!-- 配置Properties属性值 -->
	<bean id = "datasource" class = "com.spring.beans.collection.DataSource">
		<property name = "properties">
			<!-- 使用props和prop子节点来为Properties属性赋值 -->
            <props>
				<prop key = "user">root</prop>
				<prop key = "psw">1234</prop>
				<prop key = "jdbcurl">jdbc:mysql</prop>
			</props>
		</property>
	</bean>

Main中的代码:

package com.spring.beans.collection;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
	public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		
		DataSource dataSource = (DataSource) ctx.getBean("datasource");
		System.out.println(dataSource);
	}
}

2. 使用utility scheme定义集合

  • 使用基本的集合标签定义集合时,不能将集合作为独立的Bean定义,导致其他Bean无法引用该集合,所以无法在不同Bean直接共享集合
  • 可以使用 util schema里的集合标签定义独立的集合Bean,需要注意的是,必须在<beans>根元素里添加util schem标签

在在xml配置util时需要映入新的命名空间:util。步骤如下:

先在xml文件编辑界面点击“Namespaces”:

在弹出页面选中util:

Xml文件配置如下:

	<!-- 通过构造方法来配置bean的属性 -->
	<bean id="car" class="com.spring.beans.Car">
	    <constructor-arg value="Audi" index="0"></constructor-arg>
	    <!-- 若字面值中包含特殊字符,可以使用<![CDATA[]]>把字面值包裹起来 -->
	    <constructor-arg index="1">
            <value><![CDATA[<Shanghai>]]></value>
        </constructor-arg>
        <!-- 属性值也可以使用value直接点进行配置 -->
	    <constructor-arg type="int">
	    	<value>3000</value>
	    </constructor-arg>
	</bean>
	<!-- 使用构造器注入属性值可以指定参数的位置和参数的类型!以区分重载的构造器 -->
	<bean id="car2" class="com.spring.beans.Car">
	    <constructor-arg value="Audi" type="java.lang.String"></constructor-arg>
	    <constructor-arg value="AShanghai" type="java.lang.String"></constructor-arg>
	    <constructor-arg value="200" type="double"></constructor-arg>
	</bean>

	<!-- 配置独立的集合bean,以供多个bean进行引用 需要导入util命名空间-->
	<util:list id = "cars">
		<ref bean = "car"/>
		<ref bean = "car2"/>
	</util:list>
	<bean id = "person4" class = "com.spring.beans.collection.Person">
		<property name="name" value="Jack"></property>
		<property name="age" value="24"></property>
		<property name="cars" ref="cars"></property>
	</bean>

属性配置细节总的代码如下:

<?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
	     class:是一个全类名,通过反射的方法在IOC容器中创建Bean的实例。所以要求在Bean中有一个无参数的构造器
	     id:Bean的名称,标识容器中的bean,id唯一
	 -->
	<bean id="helloworld" class="com.spring.beans.HelloWorld"> 
	
	    <property name="name" value="Spring"></property>
	</bean>  
	
	<!-- 通过构造方法来配置bean的属性 -->
	<bean id="car" class="com.spring.beans.Car">
	    <constructor-arg value="Audi" index="0"></constructor-arg>
	    <!-- 若字面值中包含特殊字符,可以使用<![CDATA[]]>把字面值包裹起来 -->
	    <constructor-arg index="1">
            <value><![CDATA[<Shanghai>]]></value>
        </constructor-arg>
        <!-- 属性值也可以使用value直接点进行配置 -->
	    <constructor-arg type="int">
	    	<value>3000</value>
	    </constructor-arg>
	</bean>
	<!-- 使用构造器注入属性值可以指定参数的位置和参数的类型!以区分重载的构造器 -->
	<bean id="car2" class="com.spring.beans.Car">
	    <constructor-arg value="Audi" type="java.lang.String"></constructor-arg>
	    <constructor-arg value="AShanghai" type="java.lang.String"></constructor-arg>
	    <constructor-arg value="200" type="double"></constructor-arg>
	</bean>
	
	<bean id = "person" class = "com.spring.beans.Person">
		<property name = "name" value = "Tom"></property>
		<property name = "age" value = "24"></property>
		<!-- 可以使用property的ref属性来建立bean之间的引用关系
		<property name = "car" ref = "car2"></property>
		-->
		<!-- 内部Bean 不能被外部Bean引用-->
		<property name="car">
			<bean class="com.spring.beans.Car">
	    		<constructor-arg value="Ford" type="java.lang.String"></constructor-arg>
	   	 		<constructor-arg value="Beijing" type="java.lang.String"></constructor-arg>
	    		<constructor-arg value="2000000" type="double"></constructor-arg>
	       </bean>
		</property>
	</bean>
	
	<bean id = "person2" class = "com.spring.beans.Person">
		<constructor-arg value="Jerry"></constructor-arg>
	   	<constructor-arg value="25"></constructor-arg>
	   	<constructor-arg ref="car"></constructor-arg>
	   	<!-- 测试赋值null 
	    			<constructor-arg ><null/></constructor-arg>
	    --> 
	    <!-- 为级联属性赋值 属性需要先初始化后才能为级联属性赋值,否则会有异常-->
	    <property name = "car.price" value = "300000"></property>
	    <property name = "car.maxSpeed" value = "250"></property>
	</bean>
	
	
	<!-- 测试如何配置集合属性 -->
	<bean id = "person3" class = "com.spring.beans.collection.Person">
		<property name = "name" value = "Mike"></property>
		<property name = "age" value = "18"></property>
		<property name = "cars">
			<!-- 使用list节点为list类型的属性赋值 -->
			<list>
				<ref bean = "car"/>
				<ref bean = "car2"/>
			</list>
		</property>
	</bean>
	
	<!-- 配置Map属性值 -->
	<bean id = "newperson" class = "com.spring.beans.collection.NewPerson">
		<property name = "name" value = "Rose"></property>
		<property name = "age" value = "19"></property>
		<property name = "cars">
		<!-- 使用map节点及map的entry子节点配置Map类型的成员变量 -->
			<map>
				<entry key = "AA" value-ref = "car"></entry>
				<entry key = "BB" value-ref = "car2"></entry>
			</map>
		</property>
	</bean>
	
	<!-- 配置Properties属性值 -->
	<bean id = "datasource" class = "com.spring.beans.collection.DataSource">
		<property name = "properties">
		<!-- 使用props和prop子节点来为Properties属性赋值 -->
			<props>
				<prop key = "user">root</prop>
				<prop key = "psw">1234</prop>
				<prop key = "jdbcurl">jdbc:mysql</prop>
			</props>
		</property>
	</bean>
	
	<!-- 配置独立的集合bean,以供多个bean进行引用 需要导入util命名空间-->
	<util:list id = "cars">
		<ref bean = "car"/>
		<ref bean = "car2"/>
	</util:list>
	<bean id = "person4" class = "com.spring.beans.collection.Person">
		<property name="name" value="Jack"></property>
		<property name="age" value="24"></property>
		<property name="cars" ref="cars"></property>
	</bean>
</beans>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值