这一章节我们来讨论一下怎样建立集合以及訪问集合的元素?

1.建立集合?

(1)domain

蛋糕类:

package com.raylee.my_new_spring.my_new_spring.ch01.topic_1_20;

public class Cake {

	private String name = "";

	private double size = 0;

	public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public double getSize() {
return size;
} public void setSize(double size) {
this.size = size;
} }

(2)配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsdhttp://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"><util:list id="cakes"><bean class="com.raylee.my_new_spring.my_new_spring.ch01.topic_1_20.Cake"p:name="blueberry cheese cake" p:size="5" scope="prototype" /><bean class="com.raylee.my_new_spring.my_new_spring.ch01.topic_1_20.Cake"p:name="chocolate cheese cake" p:size="6" scope="prototype" /><bean class="com.raylee.my_new_spring.my_new_spring.ch01.topic_1_20.Cake"p:name="banana oatmel mousse cake" p:size="7" scope="prototype" /><bean class="com.raylee.my_new_spring.my_new_spring.ch01.topic_1_20.Cake"p:name="vanilla eclair" p:size="8" scope="prototype" /><bean class="com.raylee.my_new_spring.my_new_spring.ch01.topic_1_20.Cake"p:name="ligueur perfumed triplet cake" p:size="5.5" scope="prototype" /></util:list></beans>

上面的配置文件须要注意的地方:

<1>引入util的命名空间。须要在头部加上:

xmlns:util="http://www.springframework.org/schema/util"

以及:

http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd

<2>使用<util:list/>标签。当spring容器启动的时候,spring会帮我们生成上面的列表

<3>除了<util:list/>还有<util:set/>和<util:map/>等经常使用容器

<4>Bean由于在容器里面。因此不须要id。当做内部类来引入

2.訪问集合的元素

(1)domain

蛋糕类:(沿用上面的代码)

package com.raylee.my_new_spring.my_new_spring.ch01.topic_1_20;

public class Cake {

	private String name = "";

	private double size = 0;

	public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public double getSize() {
return size;
} public void setSize(double size) {
this.size = size;
} }

厨师类:(为了方便,我们减轻一下属性)

package com.raylee.my_new_spring.my_new_spring.ch01.topic_1_20;

public class Chief {
private Cake cake = null; private String name = ""; public Cake getCake() {
return cake;
} public String getName() {
return name;
} public Cake makeOneCake() {
return cake;
} public void setCake(Cake cake) {
this.cake = cake;
} public void setName(String name) {
this.name = name;
} }

(2)測试类:

package com.raylee.my_new_spring.my_new_spring.ch01.topic_1_20;

import java.util.ArrayList;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
"/com/raylee/my_new_spring/my_new_spring/ch01/topic_1_20/ApplicationContext-test.xml" })
public class CakeTest { @Autowired
private ApplicationContext applicationContext; @Test
public void testChief() {
@SuppressWarnings("unchecked")
ArrayList<Cake> cakes = (ArrayList<Cake>) applicationContext.getBean("cakes");
System.out.println(cakes.size());
Chief jack=applicationContext.getBean(Chief.class);
Cake cake =jack.getCake();
System.out.println(cake.getName());
}
}

(3)配置文件

<?xml version="1.0" encoding="UTF-8"?

>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <util:list id="cakes">
<bean class="com.raylee.my_new_spring.my_new_spring.ch01.topic_1_20.Cake"
p:name="blueberry cheese cake" p:size="5" scope="prototype" /> <bean class="com.raylee.my_new_spring.my_new_spring.ch01.topic_1_20.Cake"
p:name="chocolate cheese cake" p:size="6" scope="prototype" /> <bean class="com.raylee.my_new_spring.my_new_spring.ch01.topic_1_20.Cake"
p:name="banana oatmel mousse cake" p:size="7" scope="prototype" /> <bean class="com.raylee.my_new_spring.my_new_spring.ch01.topic_1_20.Cake"
p:name="vanilla eclair" p:size="8" scope="prototype" /> <bean class="com.raylee.my_new_spring.my_new_spring.ch01.topic_1_20.Cake"
p:name="ligueur perfumed triplet cake" p:size="5.5" scope="prototype" />
</util:list> <bean id="jack"
class="com.raylee.my_new_spring.my_new_spring.ch01.topic_1_20.Chief"
p:name="jack">
<property name="cake" value="#{cakes[1]}" />
</bean>
</beans>

配置文件的注意点:

<1>我们须要使用前面的#{}表达式

<2>在属性注入的时候,不再是ref。由于找不到对应的bean,直接使用value,由于我们通过表达式返回的是一个对象

測试输出:

5
chocolate cheese cake

总结:这一章节主要介绍了怎样建立集合以及訪问集合的元素。

文件夹:http://blog.csdn.net/raylee2007/article/details/50611627

我的github:https://github.com/raylee2015/my_new_spring

从头认识Spring-1.16 SpEl对集合的操作(1)-建立集合以及訪问集合的元素,以&lt;util:list/&gt;为例的更多相关文章

  1. 从头认识Spring-1.15 对SpEl的值的操作(1)-数值运算

    这一章节我们来讨论一下对SpEl的值的运算. 1.domain 烤炉类:(不变) package com.raylee.my_new_spring.my_new_spring.ch01.topic_1 ...

  2. Spring MVC学习-------------訪问到静态的文件

    怎样訪问到静态的文件,如jpg,js,css? 怎样你的DispatcherServlet拦截"*.do"这种有后缀的URL.就不存在訪问不到静态资源的问题. 假设你的Dispat ...

  3. Spring表达式语言SpEL简单介绍

    Spring3引入了Spring表达式语言(Spring Expression Language,SpEL). SpEL有非常多特性.比較经常使用的包含: 1.使用bean的id来引用bean, 以下 ...

  4. Spring学习笔记--Spring表达式语言SpEL

    Spring3引入了Spring表达式语言(Spring Expression Language,SpEL).SpEL是一种强大的.简洁的装配Bean的方式,它通过运行期执行的表达式将值装配到Bean ...

  5. Spring 缓存注解 SpEL 表达式解析

    缓存注解上 key.condition.unless 等 SpEL 表达式的解析 SpEl 支持的计算变量: 1)#ai.#pi.#命名参数[i 表示参数下标,从 0 开始] 2)#result:Ca ...

  6. 【Spring】16、注解事务 @Transactional

    概述 事务管理对于企业应用来说是至关重要的,即使出现异常情况,它也可以保证数据的一致性.Spring Framework对事务管理提供了一致的抽象,其特点如下: 为不同的事务API提供一致的编程模型, ...

  7. Python基础2 列表 元祖 字符串 字典 集合 文件操作 -DAY2

    本节内容 列表.元组操作 字符串操作 字典操作 集合操作 文件操作 字符编码与转码 1. 列表.元组操作 列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储.修改等操作 定义列表 ...

  8. redis 的使用 (sort set排序集合类型操作)

    sort set排序集合类型 释义: sort set 是 string 类型的集合 sort set 的每个元素 都会关联一个 权 通过 权值 可以有序的获取集合中的元素 应用场合: 获取热门帖子( ...

  9. paip.数组以及集合的操作uapi java php python总结..

    paip.数组以及集合的操作uapi 作者Attilax  艾龙,  EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn.net/att ...

随机推荐

  1. android studio2.2 配置NDK

    1.配置环境: Android studio2.2 配置NDK NDK版本[android-ndk-r13b-windows-x86_64.zip] NDK下载网址:[https://dl.googl ...

  2. 2015 Multi-University Training Contest 6 hdu 5361 In Touch

    In Touch Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total ...

  3. 洛谷 P2507 [SCOI2008]配对

    P2507 [SCOI2008]配对 题目背景 四川NOI2008省选 题目描述 你有 n 个整数Ai和n 个整数Bi.你需要把它们配对,即每个Ai恰好对应一个Bp[i].要求所有配对的整数差的绝对值 ...

  4. echarts 设置x轴的和y轴的属性

    axisLabel:{ interval:0,//横轴信息全部显示 rotate:-30,//-30度角倾斜显示 } splitNumber:10

  5. Android程序之全国天气预报查询(聚合数据开发)

    一.项目演示效果例如以下: 项目源码下载地址: http://pan.baidu.com/s/1pL6o5Mb password:5myq 二.使用 聚合数据SDK: (1)聚合数据官网地址:http ...

  6. C++模板中的静态

    #include <iostream> #include <stdlib.h> using namespace std; template<class T> cla ...

  7. jdk1.8Option类

    目的:为了解决一个方法返回的参数可能为空而无法传入到新的方法做参数的问题,java8产生了新的内容:Option. 定义:Option是一个可以为空的容器对象(注意本质上是个万能对象). 常用方法:1 ...

  8. Hadoop自学笔记(一)常见Hadoop相关项目一览

    本自学笔记来自于Yutube上的视频Hadoop系列.网址: https://www.youtube.com/watch?v=-TaAVaAwZTs(当中一个) 以后不再赘述 自学笔记,难免有各类错误 ...

  9. React-Native系列Android——Native与Javascript通信原理(一)

    React-Native最核心的是Native与Javascript之间的通信,并且是双向通信.Native层到Javascript层,Javascript层到Native层.虽说是两个方向,但实现上 ...

  10. Linux命令locate

    centos安装locate命令 centos6.3刚初始化安装完毕,有个配置文件不知道存在什么地方,想用locate命令来查找下,发现系统提示,找不到该命令.以前经常用的命令为什么找不到了呢???原 ...