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 ...
随机推荐
- P4868 Preprefix sum
传送门 挺显然的一题?单点修改,前缀和数组前缀查询 树状数组就可以维护了 考虑每个位置对应询问的贡献,设询问的位置为 $x$,对于原数组 $a[i]$ 的某个位置 $i$,它会贡献 $(x-i+1)* ...
- spring boot 是如何加载jackson的?
Spring Boot 自动引入jackson: 通过:Spring-Boot-starter-web Jackson自动配置 这里的configurations是读取的这里: 通过反射加载Jacks ...
- 关于python3.4版本中的zip函数
特别注意: 在window,显示变量 print(x);而在linux中 print x 例如,有两个列表: >>>a = [1,2,3] >>>b = [4,5, ...
- Shiro多Realm验证
在单Realm的基础上,进行如下内容的修改 原有的ShiroRealm.java: package com.atguigu.shiro.realms; import org.apache.shiro. ...
- pycharm 激激活码
pycharm 激活码 [20191224亲测可用] http://lookdiv.com/neet/youd/codes.html
- last, lastb - 显示最近登录的用户列表
总览 last [-R] [-num] [ -n num ] [-adiox] [ -f file ] [name...] [tty...] lastb [-R] [-num] [ -n num ] ...
- poj 1915 KnightMoves(bfs)
Knight Moves Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 24094 Accepted: 11364 De ...
- Laravel 中 Controller访问Model函数/常量
<?php // User.php class User extends Model { ; //进行中 const USER_TYPE_TEST = 'test'; //测试用户 // 需要在 ...
- Java JDK下载方法
https://jingyan.baidu.com/album/574c5219fb033c2c8d9dc194.html?picindex=5 也可以参考这个 ‘’‘’ 大家下载的时候一定要按照步 ...
- C#基础知识之图解TCP IP》读书笔记
一.网络基础知识 1. 计算机使用模式的演变 2.协议 协议就是计算机与计算机之间通过网络实现通信事先达成的一种“约定”.这种“约定”使那些由不同厂商的设备.不同的CPU以及不同的操作系统组成的计算机 ...