当前位置: 代码迷 >> 综合 >> spring IOC List,Map ,props,util:list等集合属性
  详细解决方案

spring IOC List,Map ,props,util:list等集合属性

热度:26   发布时间:2023-11-24 14:10:54.0

接着咱们的spring系列博客继续走。
配置到了一个人可以有一辆车,但是如果一个人也可以有几辆车。没办法,就是有钱。
那怎么办了。有办法,用集合属性。
定义好list类型的对象cars,重写setter和getter方法。和toString函数。

private List<Car> cars;
public List<Car> getCars() {return cars;}public void setCars(List<Car> cars) {this.cars = cars;}@Overridepublic String toString() {return "Person [name=" + name + ", age=" + age + ", cars=" + cars + "]";}

写好配置文件
首先写好几个car的bean
然后在person里面引入写好的car bean

<bean id="person2" class="com.beans.collection.Person"><property name="name" value="jerry"></property><property name="age" value="23"></property><property name="cars" ><list><ref bean="car"/><ref bean="car2"/></list></property></bean>

运行结果如下图
在这里插入图片描述
用set的话定义方法使用和list是差不多的。这里就不再赘述了。

关于map

private Map<String, Car> cars;
public Map<String, Car> getCars() {return cars;}public void setCars(Map<String, Car> cars) {this.cars = cars;}@Overridepublic String toString() {return "Person [name=" + name + ", age=" + age + ", cars=" + cars + "]";}

配置文件如下所示

<!-- 使用map节点以及map的entry子节点配置map类型的成员变量 --><property name="cars"><map><entry key="aa" value-ref="car"></entry><entry key="bb" value-ref="car2"></entry></map></property>

运行如下
在这里插入图片描述
关于
新建一个类测试一下。

package com.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;}@Overridepublic String toString() {return "DataSource [properties=" + properties + "]";}}

配置文件

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

在运行主类写上

ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
DataSource dataSource=ctx.getBean(DataSource.class);System.out.println(dataSource);

运行如下
在这里插入图片描述
使用utility schema定义集合
使用基本的集合标签定义集合时,不能将集合作为独立的bean定义,导致其他bean无法引用该集合,所以无法在不同bean之间共享集合。
可以使用util schema里的集合标签定义独立的集合bean,需要注意的是,必须在根元素里添加util schema定义

加入xmlns:util=“http://www.springframework.org/schema/util”
和 http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
如下图所示

<?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.xsdhttp://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util.xsd">

配置文件

<!-- 配置单例的集合bean ,以供多个bean进行引用 --><util:list id="cars"><ref bean="car"/><ref bean="car2"/></util:list><bean id="person2" class="com.beans.collection.Person"><property name="name" value="jerry"></property><property name="age" value="23"></property><property name="cars" ref="cars"></property></bean>

运行如下
在这里插入图片描述

  相关解决方案