Spring框架xml配置文件 复杂类型属性注入——数组 list map properties DI dependency injection 依赖注入——属性值的注入依赖于建立的对象(堆空间)
Person类中的各种属性写法如下:
package com.swift.person; import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties; public class Person {
//普通字符串
private String name;
//字符串数组
private String[] arr;
//字符串列表
private List<String> list;
//键值对都为字符串的Map集合
private Map<String,String> map;
public void setMap(Map<String, String> map) {
this.map = map;
}
//这还有一个键值对的属性类
private Properties properties;
public void setName(String name) {
this.name = name;
}
public void setArr(String[] arr) {
this.arr = arr;
}
public void setList(List<String> list) {
this.list = list;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
public String fun() {
return "Person [name=" + name + ", arr=" + Arrays.toString(arr) + ", list=" + list + ", map=" + map
+ ", properties=" + properties + "]";
}
}
在xml配置文件中按如下方式配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- IoC 控制反转 Spring根据XML配置文件注入复杂类型属性 -->
<bean id="person" class="com.swift.person.Person">
<property name="name" value="swift"></property>
<property name="arr">
<array>
<value>西施</value>
<value>貂蝉</value>
<value>王昭君</value>
<value>杨玉环</value>
</array>
</property>
<property name="list">
<list>
<value>汪精卫</value>
<value>周恩来</value>
<value>梅兰芳</value>
<value>张学良</value>
</list>
</property>
<property name="map">
<map>
<entry key="name" value="swift"></entry>
<entry key="profession" value="programmer"></entry>
<entry key="city" value="beijing"></entry>
</map>
</property>
<property name="properties">
<props>
<prop key="1">冬天里的一把火</prop>
<prop key="2">逆流成河</prop>
<prop key="3">男儿当自强</prop>
</props>
</property>
</bean>
</beans>
调用的实现类如下:
package com.swift.servlet; import java.io.IOException; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.swift.person.Person;
@WebServlet("/person")
public class ServletPerson extends HttpServlet {
private static final long serialVersionUID = 1L;
public ServletPerson() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
response.getWriter().append("Served at: ").append(request.getContextPath());
ApplicationContext context=new ClassPathXmlApplicationContext("context.xml");
Person per=(Person) context.getBean("person");
String personInfo=per.fun();
response.getWriter().append(personInfo);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
} }
浏览器显示效果如下:

重新编辑上边内容
User类
注意有多个有参构造
package cn.itcast.entity;
public class User {
private String name;
private Integer age;
private Car car;
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(String name, Integer age) {
super();
this.name = name;
this.age = age;
}
public User(String name, Integer age, Car car) {
System.out.println("public User(String name, Integer age, Car car)");
this.name = name;
this.age = age;
this.car = car;
}
public User(Integer age,String name, Car car) {
System.out.println("public User(Integer age,String name, Car car)");
this.name = name;
this.age = age;
this.car = car;
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + ", car=" + car + "]";
}
}
Car类
package cn.itcast.entity;
public class Car {
private String name;
public Car() {
super();
// TODO Auto-generated constructor stub
}
public Car(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Car [name=" + name + "]";
}
}
乱七八糟集合类 (等待注入)
package cn.itcast.entity; import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties; public class InjectionCollectionMap { private Object[] array;
private List list;
private Map map;
private Properties properties;
public Object[] getArray() {
return array;
}
public void setArray(Object[] array) {
this.array = array;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public Map getMap() {
return map;
}
public void setMap(Map map) {
this.map = map;
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
@Override
public String toString() {
return "InjectionCollectionMap [array=" + Arrays.toString(array) + ", list=" + list + ", map=" + map
+ ", properties=" + properties + "]";
} }
分模块配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!-- 导入schema
约束的位置在:
..\spring-framework-4.2.4.RELEASE\docs\spring-framework-reference\html\xsd-configuration.html
文件中。
注意:要导入schema约束
-->
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- <bean name="user" class="cn.itcast.entity.User">
<property name="name" value="spring"></property>
<property name="age" value="18"></property>
<property ref="car" name="car"></property> </bean> --> <bean name="user" class="cn.itcast.entity.User">
<constructor-arg name="name" value="gaoyang" index="0" type="java.lang.String"></constructor-arg>
<constructor-arg name="age" value="26" index="1" type="java.lang.Integer"></constructor-arg>
<constructor-arg name="car" ref="car" index="2" ></constructor-arg>
</bean> <bean name="car" class="cn.itcast.entity.Car" p:name="布加迪威航">
</bean> <bean name="injectionCollectionMap" class="cn.itcast.entity.InjectionCollectionMap">
<property name="array">
<array>
<value>1</value>
<value>2</value>
<value>3</value>
<value>4</value>
<value>5</value>
</array>
</property>
<property name="list">
<list>
<value>zhangsan</value>
<value>lisi</value>
<value>wangwu</value>
<value>zhaoliu</value>
<value>tianqi</value>
</list>
</property>
<property name="map">
<map>
<entry key="name" value="gaoyang"></entry>
<entry key="car" value-ref="car"></entry>
</map>
</property>
<property name="properties">
<props>
<prop key="cold">outside is raining</prop>
<prop key="humid">outside is raining</prop>
</props>
</property>
</bean> </beans>
主配置文件中引入分模块
<?xml version="1.0" encoding="UTF-8"?>
<!-- 导入schema
约束的位置在:
..\spring-framework-4.2.4.RELEASE\docs\spring-framework-reference\html\xsd-configuration.html
文件中。
注意:要导入schema约束
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean name="iCustomerDao" class="cn.itcast.dao.impl.CustomerDaoImpl"></bean>
<bean name="iCustomerService" class="cn.itcast.service.impl.CustomerServiceImpl"></bean> <import resource="/cn/itcast/property/property_injection.xml"/> </beans>
测试文件
package cn.itcast.property; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.itcast.entity.InjectionCollectionMap;
import cn.itcast.entity.User; public class TestProperty { public static void main(String[] args) { ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
User user = (User) context.getBean("user");
InjectionCollectionMap injectionCollectionMap = (InjectionCollectionMap) context.getBean("injectionCollectionMap");
System.out.println(user);
System.out.println(injectionCollectionMap); } }
Spring框架xml配置文件 复杂类型属性注入——数组 list map properties DI dependency injection 依赖注入——属性值的注入依赖于建立的对象(堆空间)的更多相关文章
- JavaWeb_(Spring框架)xml配置文件
系列博文 JavaWeb_(Spring框架)xml配置文件 传送门 JavaWeb_(Spring框架)注解配置 传送门 Xml配置 a)Bean元素:交由Spring管理的对象都要配置在bean ...
- Spring根据XML配置文件注入对象类型属性
这里有dao.service和Servlet三个地方 通过配过文件xml生成对象,并注入对象类型的属性,降低耦合 dao文件代码: package com.swift; public class Da ...
- Spring框架的配置文件
Spring框架的配置文件 (2014-12-18 20:43:42) 转载▼ 标签: 配置文件 例子 构造函数 成员 spring 分类: 专业知识 (注:文中的"<"均需 ...
- Spring 通过XML配置文件以及通过注解形式来AOP 来实现前置,环绕,异常通知,返回后通知,后通知
本节主要内容: 一.Spring 通过XML配置文件形式来AOP 来实现前置,环绕,异常通知 1. Spring AOP 前置通知 XML配置使用案例 2. Spring AOP ...
- (转)在编写Spring框架的配置文件时,标签无提示符的解决办法
http://blog.csdn.net/yerenyuan_pku/article/details/52831618 问题描述 初学者在学习Spring框架的过程中,大概会碰到这样一个问题:在编写S ...
- [error] eclipse编写spring等xml配置文件时只有部分提示,tx无提示
eclipse编写spring等xml配置文件时只有<bean>.<context>等有提示,其他标签都没有提示 这时就需要做以下两步操作(下面以事务管理标签为例) 1,添加命 ...
- 如何配置多个Spring的xml配置文件(多模块配置)
如何使用多个Spring的xml配置文件(多模块配置) (2009-08-22 13:42:43) 如何使用多个Spring的xml配置文件(多模块配置) 在用Struts Spring Hibe ...
- spring in action学习笔记一:DI(Dependency Injection)依赖注入之CI(Constructor Injection)构造器注入
一:这里先说一下DI(Dependency Injection)依赖注入有种表现形式:一种是CI(Constructor Injection)构造方法注入,另一种是SI(Set Injection) ...
- 理解依赖注入(DI - Dependency Injection)
系列教程 Spring 框架介绍 Spring 框架模块 Spring开发环境搭建(Eclipse) 创建一个简单的Spring应用 Spring 控制反转容器(Inversion of Contro ...
随机推荐
- bzoj3453: tyvj 1858 XLkxc(拉格朗日插值)
传送门 \(f(n)=\sum_{i=1}^ni^k\),这是自然数幂次和,是一个以\(n\)为自变量的\(k+1\)次多项式 \(g(n)=\sum_{i=1}^nf(i)\),因为这东西差分之后是 ...
- 毕业N年后,请不要像我一样被档案烦死
目录 一. 提醒大学生:深刻重视档案,避免以后麻烦! 二.说说我因为档案造成的烦心事! 三.说说档案这档子事: 四.档案如此重要,为什么有些人却成了弃档族? 五.档案该怎么操作才能不当"弃档 ...
- button 获取 cell
- (void)cellBtnClicked:(id)sender event:(id)event { NSSet *touches =[event allTouches]; ...
- MyBatist庖丁解牛(二)
站在巨人的肩膀上 https://blog.csdn.net/xiaokang123456kao/article/details/76228684 一.概述 我们知道,Mybatis实现增删改查需要进 ...
- 集合:set
set 就是数学上的集合——每个元素最多只出现一次.和sort一样,自定义一个类型也可以构造set ,但是必须定义“小于”运算符. 例子: 输入一个文本,找出所有不同的单词(连续的字母序列),按字典从 ...
- pip 参数
pip 自带参数 pip --help pip install 自带参数 pip install --help
- 分布式通信-tcp/ip socket
Socket通讯的过程 Server端Listen(监听)某个端口是否有连接请求,Client端向Server 端发出Connect(连接)请求,Server端向Client端发回Accept(接受) ...
- UVa-11582:Colossal Fibonacci Numbers!(模算术)
这是个开心的题目,因为既可以自己翻译,代码又好写ヾ(๑╹◡╹)ノ" The i’th Fibonacci number f(i) is recursively defined in the f ...
- 174. 删除链表中倒数第n个节点
描述 笔记 数据 评测 给定一个链表,删除链表中倒数第n个节点,返回链表的头节点. 注意事项 链表中的节点个数大于等于n 您在真实的面试中是否遇到过这个题? Yes 样例 给出链表1->2-&g ...
- centos7 更换jdk版本
查看java版本 java -version 如果有java版本(如果没有直接看红色虚线以下的) 输入 rpm -qa | grep java会显示以下几条内容: ******* ******** ...