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

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. 【codeforces 734F】Anton and School

    [题目链接]:http://codeforces.com/problemset/problem/734/F [题意] 给你两个数组b和c; 然后让你找出一个非负数组a满足题中所给关系; [题解] 有个 ...

  2. ajax的两种使用方式

    一.Ajax概述 1.什么是同步,什么是异步 同步现象:客户端发送请求到服务器端,当服务器返回响应之前,客户端都处于等待 卡死状态 异步现象:客户端发送请求到服务器端,无论服务器是否返回响应,客户端都 ...

  3. 【动态树问题】LCT学习笔记

    我居然还不会LCT QAQ真是太弱了 必须学LCT QAQ ------------------线割分是我www------------ LinkCut-Tree是基于Splay(由于Splay能够非 ...

  4. 安卓ProgressBar水平进度条的颜色设置

    安卓系统提供了水平进度条ProgressBar的样式,而我们在实际开发中,差点儿不可能使用默认的样式.原因就是"太丑"^_^ 所以我们在很多其它的时候须要对其颜色进行自己定义,主要 ...

  5. JAVA配置Tomcat

    1.下载tomcat,我jdk是1.8的,网上查了一下,说要安装tomcat8及以上的tomcat 尝试点击,弹出, 2.配置环境 3.安装通过cmd安装 4.点击开启服务 5.输入localhost ...

  6. iOS七种手势

    // 初始化一个UIimageView UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, ...

  7. m_Orchestrate learning system---十一、thinkphp查看临时文件的好处是什么

    m_Orchestrate learning system---十一.thinkphp查看临时文件的好处是什么 一.总结 一句话总结:可以知道thinkphp的标签被smarty引擎翻译而来的php代 ...

  8. Scala语言

    一.Scala概述 Scala简介 Scala是一种针对JVM将函数和面向对象技术组合在一起的编程语言.所以Scala必须要有JVM才能运行,和Python一样,Scala也是可以面向对象和面向函数的 ...

  9. 安装Oracle RAC 11g

    1.Oracle Enterprise Linux 和 iSCSI 上构建 Oracle RAC 11g 集群 2.Oracle RAC 的所有共享磁盘存储将基于 iSCSI,iSCSI 使用在第三个 ...

  10. weboffice7

    document.all.WebOffice1.ShowToolBar = false;