从头认识Spring-1.16 SpEl对集合的操作(1)-建立集合以及訪问集合的元素,以<util:list/>为例

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

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


原文地址:https://www.cnblogs.com/claireyuancy/p/7027548.html