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 依赖注入——属性值的注入依赖于建立的对象(堆空间)的更多相关文章

  1. JavaWeb_(Spring框架)xml配置文件

    系列博文 JavaWeb_(Spring框架)xml配置文件  传送门 JavaWeb_(Spring框架)注解配置 传送门 Xml配置 a)Bean元素:交由Spring管理的对象都要配置在bean ...

  2. Spring根据XML配置文件注入对象类型属性

    这里有dao.service和Servlet三个地方 通过配过文件xml生成对象,并注入对象类型的属性,降低耦合 dao文件代码: package com.swift; public class Da ...

  3. Spring框架的配置文件

    Spring框架的配置文件 (2014-12-18 20:43:42) 转载▼ 标签: 配置文件 例子 构造函数 成员 spring 分类: 专业知识 (注:文中的"<"均需 ...

  4. Spring 通过XML配置文件以及通过注解形式来AOP 来实现前置,环绕,异常通知,返回后通知,后通知

    本节主要内容: 一.Spring 通过XML配置文件形式来AOP 来实现前置,环绕,异常通知     1. Spring AOP  前置通知 XML配置使用案例     2. Spring AOP   ...

  5. (转)在编写Spring框架的配置文件时,标签无提示符的解决办法

    http://blog.csdn.net/yerenyuan_pku/article/details/52831618 问题描述 初学者在学习Spring框架的过程中,大概会碰到这样一个问题:在编写S ...

  6. [error] eclipse编写spring等xml配置文件时只有部分提示,tx无提示

    eclipse编写spring等xml配置文件时只有<bean>.<context>等有提示,其他标签都没有提示 这时就需要做以下两步操作(下面以事务管理标签为例) 1,添加命 ...

  7. 如何配置多个Spring的xml配置文件(多模块配置)

    如何使用多个Spring的xml配置文件(多模块配置) (2009-08-22 13:42:43)   如何使用多个Spring的xml配置文件(多模块配置) 在用Struts Spring Hibe ...

  8. spring in action学习笔记一:DI(Dependency Injection)依赖注入之CI(Constructor Injection)构造器注入

    一:这里先说一下DI(Dependency Injection)依赖注入有种表现形式:一种是CI(Constructor Injection)构造方法注入,另一种是SI(Set Injection) ...

  9. 理解依赖注入(DI - Dependency Injection)

    系列教程 Spring 框架介绍 Spring 框架模块 Spring开发环境搭建(Eclipse) 创建一个简单的Spring应用 Spring 控制反转容器(Inversion of Contro ...

随机推荐

  1. UVA12504【C++STL运用】

    雨巨的UVA的C++题集英文真长- 题意: 有两本字典,第一行是旧字典,第二行是新字典. 每行不超过100个字符,没有空格,两本字典都可以是空的: 新key:+ 缺key:- 值变 :* 思路: 具体 ...

  2. Alcatraz -- 一个神奇的管理插件的Xcode插件

    Install Paste this into your terminal: curl -fsSL https://raw.githubusercontent.com/supermarin/Alcat ...

  3. Linux权限相关

    权限分组 用户:文件所有者 用户组:多个用户的集合 其他:除了用户和用户组之外的任何用户 权限类别 r:表示读的权限 w:表示写的权限 x:表示执行的权限 s:表示setuid权限,允许用户以其拥有者 ...

  4. MySQL - 执行sql报错USING BTREE

    问题与分析 在执行sql文件时发现报错如下: You have an error in your SQL syntax; check the manual that corresponds to yo ...

  5. nginx 一些配置

    worker_processes 4; #工作进程数 events { #epoll是多路复用IO(I/O Multiplexing)中的一种方式, #仅用于linux2.6以上内核,可以大大提高ng ...

  6. bzoj1492 [NOI2007]货币兑换Cash【cdq分治】

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1492 推荐博客:http://www.cnblogs.com/zig-zag/archive ...

  7. 084 Largest Rectangle in Histogram 柱状图中最大的矩形

    给出 n 个非负整数来表示柱状图的各个柱子的高度,每个柱子紧挨彼此,且宽度为 1 .您的函数要能够求出该柱状图中,能勾勒出来的最大矩形的面积. 详见:https://leetcode.com/prob ...

  8. 使用aptana执行jruby

    Apatana Studio只会找ruby/bin的ruby执行档....为了在Apatana Studio用JRuby,除了设定好Path之外还要在JRuby/bin下建立一的ruby.bat,里面 ...

  9. 用vue.js实现购物车功能

    购物车是电商必备的功能,可以让用户一次性购买多个商品,常见的购物车实现方式有如下几种: 1. 用户更新购物车里的商品后,页面自动刷新. 2. 使用局部刷新功能,服务器端返回整个购物车的页面html 3 ...

  10. equals方法那些事

    1.Equals 很多人对equals方法的用法有些模糊,这里来为大家梳理下: 字符串中的equals方法,该方法用来判断两个字符串的内容是否相同. 例1: String str1="Hel ...