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 ...
随机推荐
- PostgreSQL-pg_ctl
命令简介 pg_ctl 启动.关闭.重启 postgres pg_ctl start [-w] [-s] [-D datadir] [-l filename] [-o options] [-p pat ...
- Codeforces 1215D. Ticket Game
传送门 博弈,发现情况有点多,分析一下把有用的状态提取出来 显然各个位置的数字是没用的,我们只要知道两边的数字和分别是多少 并且状态显然和左右两边的 "?" 数量有关 因为最终我们 ...
- crm客户资源显示控制
为便于员工之间的良性竞争,避免恶意挖客户,对于不同的登录用户,在客户列表中只显示当用用户自己所拥有的客户列表. ---具体的,通过在列表显示界面的列表查询语句中增加根据用户id查询其对应的客户资源的条 ...
- vue点击出现蒙版
需求: 1.点击一个事件时弹出一个蒙版: 2.蒙版上有取消,删除事件:(点击取消时候蒙版消失,点击删除时,删除蒙版并消失): 3.点击空白地方,蒙版也消失: <template> ...
- js node 节点 原生遍历 createNodeIterator
1.createIterator msn: https://developer.mozilla.org/en-US/docs/Web/API/Document/createNodeIterator v ...
- C++ 程序设计语言
好记性不如烂笔头. 第六章 标准库给出了静态断言,形式类似如下: stastic_assert(A,S);//当A不为true时,把S作为一条编译器错误信息输出 其最重要的用途是为泛型编程中作为形参的 ...
- IDeajCommunity 配置smart tomcat插件
ntelliJ IDEA社区版没有自带tomcat. 装插件--smart tomcat. IntelliJ IDEA>>Preferences>>Plugins>> ...
- nslookup 工具的使用方法记录
查询IP地址 nslookup最简单的用法就是查询域名对应的IP地址,包括A记录和CNAME记录,如果查到的是CNAME记录还会返回别名记录的设置情况.其用法是: nslookup 域名 定查询记录类 ...
- php strip_tags() 函数去除 HTML、XML 以及 PHP 的标签。
strip_tags() 函数剥去 HTML.XML 以及 PHP 的标签.strip_tags(string,allow)参数 描述string 必需.规定要检查的字符串.allow ...
- enums应用详解
枚举类: 获取枚举相关值: