Spring各种类型数据的注入
直接上代码:
一个MessageBean类
package com.henu.spring;
import java.util.*;
public class MessageBean { private String username; //用户名
private String fileDir; //上传路径
private Set<String> types;//允许上传类型
private List<String> hbms;
private Set<String> city;
private Map<String, String> books;
private Properties props; //注入一个字符串,分析之后给其赋值
public void setTypes(String str){
String[] arr = str.split(",");
types = new HashSet<String>();
for (String string : arr) {
types.add(string);
}
} public void setUsername(String username) {
this.username = username;
}
public void setFileDir(String fileDir) {
this.fileDir = fileDir;
}
public void setCity(Set<String> city) {
this.city = city;
}
public void setHbms(List<String> hbms) {
this.hbms = hbms;
}
public void setBooks(Map<String, String> books) {
this.books = books;
}
public void setProps(Properties props) {
this.props = props;
} public void show(){
System.out.println("用户名:" + username);
System.out.println("上传路径" + fileDir); System.out.println("--hbm文件如下--");
for (String string : hbms) {
System.out.println(string);
} System.out.println("--city如下--");
for (String string : city) {
System.out.println(string);
} System.out.println("--图书信息显示--");
Set set = books.keySet();
for (Object object : set) {
System.out.println(object+" "+books.get(object));
} System.out.println("--props参数如下--");
Set<String> key = props.stringPropertyNames();
Iterator<String> iterator = key.iterator();
while(iterator.hasNext()){
String string = (String) iterator.next();
System.out.println(string+" "+props.getProperty(string));
} System.out.println("--允许上传文件类型--");
for (String string : types) {
System.out.println(string);
}
}
}
一个测试类
/**
*
*/
package com.henu.spring; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* @author Administrator
*
*/
public class TestInjection {
@Test
public void test1(){
ApplicationContext context =
new ClassPathXmlApplicationContext("/applicationContext.xml");
MessageBean messageBean = (MessageBean) context.getBean("messageBean");
messageBean.show();
}
}
一个配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <!-- 各种数据类型的注入 -->
<bean id="messageBean" class="com.henu.spring.MessageBean">
<property name="username" value="root"></property>
<property name="fileDir" value="D:\\images"></property>
<property name="types" value="jdp,gif,ipeg"></property> <property name="hbms">
<list>
<value>lengzuai nei hou !</value>
<value>lengnei nei hou !</value>
</list>
</property>
<property name="city">
<set>
<value>郑州</value>
<value>开封</value>
</set>
</property>
<property name="books">
<map>
<entry key="1" value="红楼梦"></entry>
<entry key="2" value="三国演义"></entry>
<entry key="3" value="西游记"></entry>
<entry key="4" value="水浒传"></entry>
</map>
</property>
<property name="props">
<props>
<prop key="hibernate.show_sql">
<!-- value -->
true
</prop>
<prop key="hibernate.format_sql">
true
</prop>
<prop key="hibernate.hibernate_sql">
true
</prop>
</props>
</property>
</bean>
</beans>
运行结果图

Spring各种类型数据的注入的更多相关文章
- spring集合类型的setter注入的一个简单例子
在项目中我们有时候会为集合类型设定一些默认的值,使用spring后,我们可以通过配置文件的配置,用setter方式为对象的集合属性提供一些默认值,下面就是一个简单的例子. 首先我们创建了一个名为Col ...
- Spring学习笔记之 Spring IOC容器(二) 之注入参数值,自动组件扫描方式,控制Bean实例化方式,使用注解方式
本节主要内容: 1. 给MessageBean注入参数值 2. 测试Spring自动组件扫描方式 3. 如何控制ExampleBean实例化方式 4. 使用注解方式重构Jdb ...
- Spring MVC自动为对象注入枚举数据
一.实现转换工厂,定义转换实现,如下: package com.mafwo; import org.springframework.core.convert.converter.Convert ...
- Spring 让 LOB 数据操作变得简单易行,LOB 代表大对象数据,包括 BLOB 和 CLOB 两种类型
转自:https://www.ibm.com/developerworks/cn/java/j-lo-spring-lob/index.html 概述 LOB 代表大对象数据,包括 BLOB 和 CL ...
- Spring、基本类型属性和集合类型属性的注入
Spring 还可以对基本属性和集合类型属性进行注入: public interface PersonIService { public String getBaseProperty(); publi ...
- spring集合类型注入
spring集合类型注入 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUB ...
- Spring学习日记02_IOC_属性注入_其他类型属性
ICO操作Bean管理(xml注入其它类型属性) 字面量 null值 <property name="address"> <null></null&g ...
- Spring:所有依赖项注入的类型
一.前言 Spring文档严格只定义了两种类型的注入:构造函数注入和setter注入.但是,还有更多的方式来注入依赖项,例如字段注入,查找方法注入.下面主要是讲使用Spring框架时可能发生的类型. ...
- 【Spring】SpringMVC中浅析Date类型数据的传递
在控制器中加入如下代码: @InitBinder public void initBinder(ServletRequestDataBinder bin){ SimpleDateFormat sdf ...
随机推荐
- vue 实践技巧合集
前言 本文纯属个人平时实践过程中的一些经验总结,算是一点点小技巧吧,不是多么高明的技术,如果对你有帮助,那么不胜荣幸. 本文不涉及罕见API使用方法等,大部分内容都是基于对vue的一些实践而已.由于涉 ...
- 简述在Ubuntu终端打开文件的几种不同方法与区别
一· 在Ubuntu下,通常用命令行打开文本文件,比如用命令gedit.more.cat.vim.less. gedit:在文本软件下打开文件,可直接修改. more ,cat 和 less :类似, ...
- Vue组件通信方式(8种)
1.一图认清组件关系名词 父子关系:A与B.A与C.B与D.C与E 兄弟关系:B与C 隔代关系:A与D.A与E 非直系亲属:D与E 总结为三大类: 父子组件之间通信 兄弟组件之间通信 跨级通信 2.8 ...
- WebView获取title更改
[self.titleLabel setText:[self.webVIew stringByEvaluatingJavaScriptFromString:@"document.title& ...
- 为Xcode配置Git和Github
Xcode.Git和Github是三个伟大的编程工具.本文记录一下如何在Xcode中使用Git作为源代码控制工具,以及如何将本地的Git仓库和远程Github上的仓库集成起来. 1. 如何为新建的Xc ...
- Linux 硬盘挂载(服务器重启自动挂载)
1.先查看目前机器上有几块硬盘,及已挂载磁盘: fdisk -l 能够查看到当前主机上已连接上的磁盘,以及已经分割的磁盘分区.(下面以/dev/vdb磁盘进行分区.挂载为例,挂载点设置为/data) ...
- Spring boot 拦截器和过滤器
1. 过滤器 Filter介绍 Filter可以认为是Servlet的一种“加强版”,是对Servlet的扩展(既可以对请求进行预处理,又可以对处理结果进行后续处理.使用Filter完整的一般流程是: ...
- 微软推出全新的Windows终端应用程序
微软正推出一款名为Windows Terminal的新命令行应用程序.它被设计为访问PowerShell,cmd.exe和Windows子系统Linux(WSL)等环境的中心位置.微软正在为想要调整终 ...
- 004-SaltStack入门篇之数据系统Grains、Pillar
1.什么是Grains? Grains是saltstack的组件,用于收集salt-minion在启动时候的信息,又称为静态信息.可以理解为Grains记录着每台Minion的一些常用属性,比如CPU ...
- openCV for python的使用
一.openCV简介 OpenCV是一个开源的跨平台计算机视觉库.它轻量级而且高效——由一系列 C 函数和少量C++类构成,同时提供了Python.Ruby.MATLAB等语言的接口,实现了图像处理和 ...