《Spring》篇章整体栏目
—————————————————————————————
【第一章】spring 概念与体系结构
【第二章】spring IoC 的工作原理
【第三章】spring IOC与Bean环境搭建与应用
【第四章】spring bean定义
【第五章】Spring 集合注入、作用域
【第六章】Spring 自动装配
【第七章】spring AOP
【第八章】Spring 集成JdbcTemplate
【第九章】Spring数据库事务管理
【第十章】Spring 集成Redis
【第十一章】Spring实战之打造新闻系统后端接口
—————————————————————————————
—————————————————————————————
前言
本章节讲解spring里如何注入集合,以及spring bean的作用域
1、注入
1.1、注入集合
标签 | 说明 |
---|---|
<list> | 用于注入 list 类型的值,允许重复 |
<set> | 用于注入 set 类型的值,不允许重复 |
<map> | 用于注入 key-value 的集合,其中 key 和 value 都可以是任意类型 |
<props> | 用于注入 key-value 的集合,其中 key 和 value 都是字符串类型 |
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* 集合注入
*/
public class CollectionDI {
/**
* 数组注入
*/
private String[] permissions;
/**
* list注入
*/
private List<String> student;
/**
* map注入
*/
private Map<String,String> maps;
/**
* Set注入
*/
private Set<String> cities;
public String[] getPermissions() {
return permissions;
}
public void setPermissions(String[] permissions) {
this.permissions = permissions;
}
public List<String> getStudent() {
return student;
}
public void setStudent(List<String> student) {
this.student = student;
}
public Map<String, String> getMaps() {
return maps;
}
public void setMaps(Map<String, String> maps) {
this.maps = maps;
}
public Set<String> getCities() {
return cities;
}
public void setCities(Set<String> cities) {
this.cities = cities;
}
@Override
public String toString() {
return "CollectionDI{" +
"permissions=" + Arrays.toString(permissions) +
", student=" + student +
", maps=" + maps +
", cities=" + cities +
'}';
}
}
<?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-3.0.xsd">
<bean id="collectionDI" class="com.xxx.spring.bean.CollectionDI">
<property name="permissions">
<array>
<value>新闻管理</value>
<value>系统管理</value>
</array>
</property>
<property name="student">
<list>
<value>张三</value>
<value>李四</value>
</list>
</property>
<property name="maps">
<map>
<entry key="1" value="军事"/>
<entry key="2" value="时事"/>
</map>
</property>
<property name="cities">
<set>
<value>北京</value>
<value>上海</value>
<value>深圳</value>
<value>广州</value>
</set>
</property>
</bean>
</beans>
集合对象为某个类型,则可用 <ref bean="xxxx”/>或者
<list>
<bean class="xxx">
<!--设置bean的属性-->
</bean>
</list>
1.2、其他注入
1.2.1、注入null或者空串
<!--使用null 标签注入 Null 值-->
<property name="propertyNull">
<null/>
</property>
<!--使用空参数注入空字符串-->
<property name="propertyEmpty" value=""></property>
1.2.2、注入字面量
<!--通过转义注入包含特殊符号的字面量-->
<property name="propertyLiteral"
value="<Java从入门到精通>">
</property>
1.2.3、使用短字符串 <![CDATA[]]>
<!--使用 <![CDATA[]]> 将包含特殊符号的字面量注入-->
<property name="propertyLiteral">
<value><![CDATA[<www.baidu.com>]]></value>
</property>
1.2.4、级联属性赋值
在 <bean> 的 <property> 子元素中,为它所依赖的 Bean 的属性进行赋值,这就是所谓的“级联属性赋值”
使用级联属性赋值时,需要注意以下 3点:
- Java 类中必须有 setter 方法;
- Java 类中必须有无参构造器(默认存在);
- 依赖其他 Bean 的类中,必须提供一个它依赖的 Bean 的 getXxx() 方法
<property name="dependBean.dependProperty"
value="级联属性赋值">
</property>
<bean id="dependBean" class="com.xxx.spring.bean.DependBean">
<!--对属性进行赋值-->
<property name="dependProperty"
value="">
</property>
</bean>