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

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. JavaScript 常见创建对象的方式

    JavaScript 有哪几种创建对象的方式? javascript创建对象简单的说,无非就是使用内置对象或各种自定义对象,当然还可以用JSON:但写法有很多种,也能混合使用. (1)对象字面量的方式 ...

  2. 将Oracle中的数据放入elasticsearch

    package com.c4c.test; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Res ...

  3. ubuntu 14.04服务器上使用nginx搭建wordpress博客详解

    过程详解 1.更新apt-get sudo apt-get update 2.安装nginx sudo apt-get install nginx 3.启动nginx sudo service ngi ...

  4. Mysql优化之优化工具profiling

    程序员的成长之路 2016-11-23 22:42 Mysql优化之优化工具profiling 前言 mysql优化技术: mysql优化不是做一个操作就可以的优化,它包含很多的细节,需要一点一点的优 ...

  5. $.ajax() 获取不到return 返回值

    /*常见错误示例 直接在 ajax 里面return 结果 */ function demo(){ $.ajax({ url : 'test.do', type : "post", ...

  6. 在对Activity基类的封装中,我做了什么

    在开发实践中,不同Activity有很多代码是反复冗余的.因此非常有必要将这部分抽取出来.封装一个继承自Activity的类,命名为BaseActivity. 翻看之前写过的代码,起初,BaseAct ...

  7. leetcode第一刷_Reverse Linked List II

    翻转链表绝对是终点项目,应该掌握的,这道题要求的是翻转一个区间内的节点.做法事实上非常相似,仅仅只是要注意判定開始是头的特殊情况,这样head要更新的,还有就是要把翻转之后的尾部下一个节点保存好,要么 ...

  8. PyQt: LineEdit的智能输入提示

    使用的的类是QtGui.QCompleter from PyQt4 import QtGui,QtCore str = QtCore.QStringList(['a','air','airbus']) ...

  9. 上传文件 nginx 413错误

    nginx : 413 Request Entity Too Large 上传文件过程发生413 Request Entity Too Large错误,翻译为请求实体过大,断定为nginx限制了请求体 ...

  10. hdu-3401-Trade-单调队列优化的DP

    单调队列入门题... dp[i][j]:第i天.手中拥有j个股票时,获得的最大利润. 若第i天不买不卖:dp[i][j]=max(dp[i][j],dp[i-1][j]); 若第i天买         ...