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

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. ASP.NET-优化websit

    如何优化一个网站 1.如果是数据库的问题则尝试添加索引.优化SQL语句,如果是算法的问题,则优化算法. 2.如果对于一些不经常改动的页面可以使用静态页技术! 3.对于一些数据不需要及时更新的而且取数据 ...

  2. test文件夹,测试类是放在src目录下的,test测试代码是代码啊,当然要放在代码文件夹下

    test文件夹,测试类是放在src目录下的,test测试代码是代码啊,当然要放在代码文件夹下 Maven的标准工程结构 Maven的标准工程结构如下: |-- pom.xml(maven的核心配置文件 ...

  3. HDU 2643

    (第二类斯特林数*N的阶乘 )的和. #include <iostream> #include <cstdio> #include <algorithm> #def ...

  4. 上机题目(0基础)-计算两个正整数的最大公约数和最小公倍数(Java)

    题目例如以下:

  5. UVA 11294 - Wedding(Two-Set)

    UVA 11294 - Wedding 题目链接 题意:有n对夫妻,0号是公主.如今有一些通奸关系(男男,女女也是可能的)然后要求人分配在两側.夫妻不能坐同一側.而且公主对面一側不能有两个同奸的人,问 ...

  6. Android开发之视图动画基础

    Android的animation由四种类型组成 XML中  alpha 渐变透明度动画效果 scale 渐变尺寸伸缩动画效果 translate 画面转换位置移动动画效果 rotate 画面转移旋转 ...

  7. UVA - 10229 Modular Fibonacci 矩阵快速幂

                                 Modular Fibonacci The Fibonacci numbers (0, 1, 1, 2, 3, 5, 8, 13, 21, 3 ...

  8. bzoj4519: [Cqoi2016]不同的最小割(分治最小割)

    4519: [Cqoi2016]不同的最小割 题目:传送门 题解: 同BZOJ 2229 基本一样的题目啊,就最后用set记录一下就ok 代码: #include<cstdio> #inc ...

  9. [jzoj 5178] [NOIP2017提高组模拟6.28] So many prefix? 解题报告(KMP+DP)

    题目链接: https://jzoj.net/senior/#main/show/5178 题目: 题解: 我们定义$f[pos]$表示以位置pos为后缀的字符串对答案的贡献,答案就是$\sum_{i ...

  10. 线上服务CPU100%问题快速定位实战--转

    来自微信公众号 架构师之路 功能问题,通过日志,单步调试相对比较好定位. 性能问题,例如线上服务器CPU100%,如何找到相关服务,如何定位问题代码,更考验技术人的功底. 58到家架构部,运维部,58 ...