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 ...
随机推荐
- spark日志+hivesql
windows本地读取hive,需要在resource里面将集群中的hive-site.xml下载下来. <?xml version="1.0" encoding=" ...
- MD5算法+盐Salt
1.MD算法的基的概念 MD5算法是典型的消息摘要算法,其前身有MD2.MD3和MD4算法,它由MD4.MD3和MD2算法改进而来.不论是哪一种MD算法,它们都需 要获得一个随机长度的信息并产生 ...
- Vue源码解析:AST语法树转render函数
开始 今天要说的代码全在codegen文件夹中,在说实现原理前,还是先看个简单的例子! <div class="container"> <span>{{ms ...
- jmeter强大的扩展插件!!
jmeter4.0以上版本,如jmeter5.1.1版本的集成插件,只需要在官网下下载“plugins-manager.jar”包,放在jmeter安装路径的lib/ext目录下即可使用. (但该ja ...
- spring boot 是如何利用jackson进行序列化的?
接上一篇:spring boot 是如何利用jackson进行反序列化的? @RestController public class HelloController { @RequestMapping ...
- 修改文件夹的所有者为www
切换到root用户:su - root 修改文件所属用户和用户组:chown 用户:用户组 文件名如果用户和用户组是www,那么需要执行命令如下:chown www:www filename -R ( ...
- ThinkPHP中header()无法发生跳转的解决办法
一定要在header('Location:url')后加上exit()/die()才可以,特此记录.
- Redis【4】Java Jedis 操作 Redis~
package redis.redis; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; /** * 描 ...
- 53道Java线程面试题
53道Java线程面试题 下面是Java线程相关的热门面试题,你可以用它来好好准备面试. 1) 什么是线程? 线程是操作系统能够进行运算调度的最小单位,它被包含在进程之中,是进程中的实际运作单位.程序 ...
- framebuffer测试程序
/* framebuffer简单测试程序 网上转载 很多次 的程序 :-) */ #include <stdio.h> #include <stdlib.h> #include ...