第五篇 Spring 集合注入、作用域

《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="&lt;Java从入门到精通&gt;">
</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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

青花科技

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值