Spring实战之注入集合值操作示例
作者:cakincqm 发布时间:2023-03-04 04:02:53
标签:Spring,注入集合值
本文实例讲述了Spring实战之注入集合值操作。分享给大家供大家参考,具体如下:
一 配置
<?xml version="1.0" encoding="GBK"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<!-- 定义2个普通Axe Bean -->
<bean id="stoneAxe" class="org.crazyit.service.impl.StoneAxe"/>
<bean id="steelAxe" class="org.crazyit.service.impl.SteelAxe"/>
<!-- 定义chinese Bean -->
<bean id="chinese" class="org.crazyit.service.impl.Chinese">
<property name="schools">
<!-- 为调用setSchools()方法配置List集合作为参数值 -->
<list>
<!-- 每个value、ref、bean...都配置一个List元素 -->
<value>小学</value>
<value>中学</value>
<value>大学</value>
</list>
</property>
<property name="scores">
<!-- 为调用setScores()方法配置Map集合作为参数值 -->
<map>
<!-- 每个entry配置一个key-value对 -->
<entry key="数学" value="87"/>
<entry key="英语" value="89"/>
<entry key="语文" value="82"/>
</map>
</property>
<property name="phaseAxes">
<!-- 为调用setPhaseAxes()方法配置Map集合作为参数值 -->
<map>
<!-- 每个entry配置一个key-value对 -->
<entry key="原始社会" value-ref="stoneAxe"/>
<entry key="农业社会" value-ref="steelAxe"/>
</map>
</property>
<property name="health">
<!-- 为调用setHealth()方法配置Properties集合作为参数值 -->
<props>
<!-- 每个prop元素配置一个属性项,其中key指定属性名 -->
<prop key="血压">正常</prop>
<prop key="身高">175</prop>
</props>
<!--
<value>
pressure=normal
height=175
</value> -->
</property>
<property name="axes">
<!-- 为调用setAxes()方法配置Set集合作为参数值 -->
<set>
<!-- 每个value、ref、bean..都配置一个Set元素 -->
<value>普通的字符串</value>
<bean class="org.crazyit.service.impl.SteelAxe"/>
<ref bean="stoneAxe"/>
<!-- 为Set集合配置一个List集合作为元素 -->
<list>
<value>20</value>
<!-- 再次为List集合配置一个Set集合作为元素 -->
<set>
<value type="int">30</value>
</set>
</list>
</set>
</property>
<property name="books">
<!-- 为调用setBooks()方法配置数组作为参数值 -->
<list>
<!-- 每个value、ref、bean...都配置一个数组元素 -->
<value>疯狂Java讲义</value>
<value>疯狂Android讲义</value>
<value>轻量级Java EE企业应用实战</value>
</list>
</property>
</bean>
</beans>
二 接口
Axe
package org.crazyit.app.service;
public interface Axe
{
// Axe接口里有个砍的方法
public String chop();
}
Person
package org.crazyit.service;
public interface Person
{
public void test();
}
三 实现
Chinese
package org.crazyit.service.impl;
import java.util.*;
import org.crazyit.service.*;
public class Chinese implements Person
{
// 下面是系列集合类型的成员变量
private List<String> schools;
private Map scores;
private Map<String , Axe> phaseAxes;
private Properties health;
private Set axes;
private String[] books;
public Chinese()
{
System.out.println("Spring实例化主调bean:Chinese实例...");
}
// schools的setter方法
public void setSchools(List schools)
{
this.schools = schools;
}
// scores的setter方法
public void setScores(Map scores)
{
this.scores = scores;
}
// phaseAxes的setter方法
public void setPhaseAxes(Map<String , Axe> phaseAxes)
{
this.phaseAxes = phaseAxes;
}
// health的setter方法
public void setHealth(Properties health)
{
this.health = health;
}
// axes的setter方法
public void setAxes(Set axes)
{
this.axes = axes;
}
// books的setter方法
public void setBooks(String[] books)
{
this.books = books;
}
// 访问上面全部的集合类型的成员变量
public void test()
{
System.out.println(schools);
System.out.println(scores);
System.out.println(phaseAxes);
System.out.println(health);
System.out.println(axes);
System.out.println(java.util.Arrays.toString(books));
}
}
StoneAxe
package org.crazyit.app.service.impl;
import org.crazyit.app.service.*;
public class StoneAxe implements Axe
{
public String chop()
{
return "石斧砍柴好慢";
}
}
SteelAxe
package org.crazyit.app.service.impl;
import org.crazyit.app.service.*;
public class SteelAxe implements Axe
{
public String chop()
{
return "钢斧砍柴真快";
}
}
四 测试类
package lee;
import org.springframework.context.*;
import org.springframework.context.support.*;
import org.crazyit.service.*;
public class BeanTest
{
public static void main(String[] args)throws Exception
{
ApplicationContext ctx = new
ClassPathXmlApplicationContext("beans.xml");
// 获取容器中Bean,并调用方法。
Person p = ctx.getBean("chinese" , Person.class);
p.test();
}
}
五 运行
Spring实例化主调bean:Chinese实例...
[小学, 中学, 大学]
{数学=87, 英语=89, 语文=82}
{原始社会=org.crazyit.service.impl.StoneAxe@6e1567f1, 农业社会=org.crazyit.service.impl.SteelAxe@56235b8e}
{血压=正常, 身高=175}
[普通的字符串, org.crazyit.service.impl.SteelAxe@59494225, org.crazyit.service.impl.StoneAxe@6e1567f1, [20, [30]]]
[疯狂Java讲义, 疯狂Android讲义, 轻量级Java EE企业应用实战]
希望本文所述对大家java程序设计有所帮助。
来源:https://blog.csdn.net/chengqiuming/article/details/101076025


猜你喜欢
- Spring的出现是为了简化 Java 程序开发,而 SpringBoot 的出现是为了简化 Spring 程序开发.SpringBoot
- C#如何安全、高效地玩转任何种类的内存之Span的本质一、what - 痛点是什么?回答这个问题前,先总结一下如何用C#操作任何类型的内存:
- 现在很多app的支付、输入密码功能,都已经开始使用自定义数字键盘,不仅更加方便、其效果着实精致。下面带着大家学习下,如何 * 微信的数字键盘,
- 详解Java中HashSet和TreeSet的区别1. HashSetHashSet有以下特点:不能保证元素的排列顺序,顺序有可能发生变化不
- 在工作过程中,需要将一个文件夹生成压缩文件,然后提供给用户下载。所以自己写了一个压缩文件的工具类。该工具类支持单个文件和文件夹压缩。放代码:
- 本文实例讲述了C#生成单页静态页简单实现方法。分享给大家供大家参考。具体方法如下:protected void BtGroup_Server
- 在action中存放数据,代码如下:@Controller // 加入到IOC容器//@RequestMapping(value="
- 在日常开发的过程中我们经常会需要调用第三方组件或者数据库,有的时候可能会因为网络抖动或者下游服务抖动,导致我们某次查询失败。这种时候我们往往
- 本文以spring-boot-maven-plugin 2.5.4为例@Mojo defaultPhase以spring-boot-mave
- 前言Java项目开发中经常要用到分页功能,现在普遍使用SpringBoot进行快速开发,而数据层主要整合SpringDataJPA和MyBa
- 在我们实际业务中,可能存在多个类之间相互调用,形成了一个复杂的网状结构。这时候就需要有一种模式去“捋顺&rdqu
- 本文实例讲述了Android编程实现简单文件浏览器功能。分享给大家供大家参考,具体如下:运行效果:布局:<LinearLayout x
- Android SharedPreferences详解获取SharedPreferences的两种方式:1 调用Context对
- 一、创建springboot项目(采用骨架方式)创建完成;我们分析下pom文件中内容:所使用到的关键依赖: <!--springBoo
- 1、说明向上转型就是把一个子类引用给一个父类引用,也就是父类引用 引用了子类的对象,即父类 父类对象 = 子类实例。此时通过父类引用变量调用
- java中的LIST在删除时,一般会用list.remove(o); 但这样往往会出现问题,先来看下面的这段代码:package com.d
- 说起空间动态、微博的点赞效果,网上也是很泛滥,各种实现与效果一大堆。而详细实现的部分,讲述的也是参差不齐,另一方面估计也有很多大侠也不屑一顾
- 本文实例讲述了JavaMail实现发送超文本(html)格式邮件的方法。分享给大家供大家参考。具体如下:附件以超文本形式,很常用,与普通的邮
- ActiviryThreadActivityThread的初始化ActivityThread即Android的主线程,也就是UI线程,Act
- 1.with 函数首先先从with函数开始,with函数接受两个参数,第一个参数可以是一个任意类型的对象,第二个参数是一个Lambda表达式