9注入集合对象
注入List
伪代码如下
<bean id="..." class="...">
<property name="someList">
<list>
<value>具体值1</value>
<value>具体值2</value>
</list>
</property>
</bean>
<bean id="..." class="...">
<property name="someList">
<list>
<ref bean="beanId"></ref>
</list>
</property>
</bean>
注入Set
伪代码如下
<bean id="..." class="...">
<property name="someSet">
<set>
<value>具体值1</value>
<value>具体值2</value>
</set>
</property>
</bean>
<bean id="..." class="...">
<property name="someSet">
<set>
<ref bean="beanId"></ref>
</set>
</property>
</bean>
注入Map
伪代码如下
<bean id="..." class="...">
<property name="someMap">
<map>
<entry key="key1" value="v1"></entry>
<entry key="key2" value-ref="beanId"></entry>
</map>
</property>
</bean>
注入Properties
伪代码如下
Properties的key和value 都是字符串
<bean id="..." class="...">
<property name="someProperties">
<props>
<prop key="k1">v1</prop>
<prop key="k2">v2</prop>
</props>
</property>
</bean>
注入集合对象实践
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.5</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.18</version>
</dependency>
</dependencies>
package com.torey.spring.ioc.entity;
import lombok.Data;
/**
* @Author http://torey611.gitee.io/li-tao-feng/
* @Email torey6061@qq.com
* @Date 2021/4/29 19:22
* @描述:计算机类
*/
@Data
public class Computer {
//品牌
private String brand;
//类型:台式、笔记本、服务器
private String type;
private String sn;
private Float price;
public Computer() {
}
public Computer(String brand, String type, String sn, Float price) {
this.brand = brand;
this.type = type;
this.sn = sn;
this.price = price;
}
}
package com.torey.spring.ioc.entity;
import lombok.Data;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
/**
* @Author http://torey611.gitee.io/li-tao-feng/
* @Email torey6061@qq.com
* @Date 2021/4/29 19:26
* @描述:
*/
@Data
public class Company {
//房间
private List<String> rooms;
private Set<String> roomsSet;
private Map<String,Computer> computers;
//保存公司基础信息
private Properties info;
}
applicationContext.xml
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="c1" class="com.torey.spring.ioc.entity.Computer">
<constructor-arg name="brand" value="联想"></constructor-arg>
<constructor-arg name="price" value="3555"></constructor-arg>
<constructor-arg name="sn" value="1001"></constructor-arg>
<constructor-arg name="type" value="笔记本"></constructor-arg>
</bean>
<bean class="com.torey.spring.ioc.entity.Computer">
<constructor-arg name="brand" value="华硕"></constructor-arg>
<constructor-arg name="price" value="3015"></constructor-arg>
<constructor-arg name="sn" value="1002"></constructor-arg>
<constructor-arg name="type" value="笔记本3"></constructor-arg>
</bean>
<bean class="com.torey.spring.ioc.entity.Computer">
<constructor-arg name="brand" value="微星"></constructor-arg>
<constructor-arg name="price" value="3055"></constructor-arg>
<constructor-arg name="sn" value="1002"></constructor-arg>
<constructor-arg name="type" value="笔记本3"></constructor-arg>
</bean>
<bean id="company" class="com.torey.spring.ioc.entity.Company">
<property name="rooms">
<list>
<value>2001-总裁办</value>
<value>2003-总经理办公室</value>
<value>2010-研发部</value>
<value>2010-研发部</value>
</list>
</property>
<property name="roomsSet">
<!--注入的是LinkedHashSet,所以是有有序的-->
<set>
<value>2001-总裁办</value>
<value>2003-总经理办公室</value>
<value>2010-研发部</value>
<value>2010-研发部</value>
</set>
</property>
<property name="computers">
<!--Set加断点可以得知:使用的是LinkedHashMap-->
<map>
<entry key="dev_1001" value-ref="c1"></entry>
<entry key="dev_1002">
<bean class="com.torey.spring.ioc.entity.Computer">
<constructor-arg name="brand" value="华为-台式机"></constructor-arg>
<constructor-arg name="price" value="6555"></constructor-arg>
<constructor-arg name="sn" value="1005"></constructor-arg>
<constructor-arg name="type" value="台式机"></constructor-arg>
</bean>
</entry>
</map>
</property>
<property name="info">
<!--一般用Properties存放一些静态的信息,key和value 必须是字符串-->
<props>
<prop key="phone">13812345678</prop>
<prop key="address">上海东方明珠</prop>
</props>
</property>
</bean>
</beans>
package com.torey.spring.ioc;
import com.torey.spring.ioc.entity.Company;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.Properties;
/**
* @Author http://torey611.gitee.io/li-tao-feng/
* @Email torey6061@qq.com
* @Date 2021/4/29 19:12
* @描述:
*/
public class SpringApplication {
public static void main(String[] args) {
ClassPathXmlApplicationContext applicationContext =
new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
Company company= applicationContext.getBean("company", Company.class);
System.out.println(company);
Properties info = company.getInfo();
String phone= info.getProperty("phone");
System.out.println(phone);
//获取容器内所有beanId数组
String[] beanNames = applicationContext.getBeanDefinitionNames();
for (String beanName : beanNames) {
System.out.println(beanName);
System.out.println("类型:"+ applicationContext.getBean(beanName).getClass());
}
}
}
导航,上一页,下一页
8利用构造方法实现对象依赖注入
10beanscope属性讲解
支持我-微信扫一扫-加入微信公众号
赞赏作者
![赞赏作者](https://img.znyd365.com/torey/Aseven/zanShang2.jpg)