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. laravel C层 (控制器)

    <?php namespace App\Http\Controllers; use App\Index; use App\Http\Controllers\Controller; class I ...

  2. 2014-5-10 NOIP模拟赛 by coolyangzc

    Problem 1 机器人(robot.cpp/c/pas) [题目描述] 早苗入手了最新的Gundam模型.最新款自然有着与以往不同的功能,那就是它能够自动行走,厉害吧. 早苗的新模型可以按照输入的 ...

  3. 阿里云物联网 .NET Core 客户端 | CZGL.AliIoTClient:4. 设备上报属性

    文档目录: 说明 1. 连接阿里云物联网 2. IoT 客户端 3. 订阅Topic与响应Topic 4. 设备上报属性 4.1 上报位置信息 5. 设置设备属性 6. 设备事件上报 7. 服务调用 ...

  4. Linux权限相关

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

  5. 怎么快速对DB里的所有email进行校验

    问题 由于业务上的需求,重新改写了校验email的正则表达式,同时DB里又迁移了其他数据库的数据,现在需要重新对DB里的所有email再校验一次,以排除掉不合法的email. DB里的数据很多,手动去 ...

  6. PostgreSQL-3-DDL数据定义语言

    1.创建/删除新的数据库 \l  查看现有数据库 \h CREATE DATABASE  查看CREATE DATABASE语句说明 \h DROP DATABASE 查看DROP DATABASE语 ...

  7. 洛谷 P2231 [HNOI2002]跳蚤

    https://www.luogu.org/problemnew/show/P2231 题意相当于:有n个位置a[1..n],每个位置可以填[1,m]中任一个整数,问共有多少种填法满足gcd(a[1] ...

  8. Appium禁止appium setting和unlock在设备上重复安装

    1.文件:/Applications/Appium.app/Contents/Resources/node_modules/appium/node_modules/appium-android-dri ...

  9. [已读]JavaScript高级程序设计(第3版)

    从去年开始看,因为太长,总是没有办法一口气把它看完,再加上它与第二版大部分一致,读起来兴致会更缺一点. 与第二版相比,它最大的改变就是增加了很多html5的内容,譬如:Object对象的一些新东西,数 ...

  10. Java微信公众平台开发(九)--微信自定义菜单的创建实现

    自定义菜单这个功能在我们普通的编辑模式下是可以直接在后台编辑的,但是一旦我们进入开发模式之后我们的自定义菜单就需要自己用代码实现,所以对于刚开始接触的人来说可能存在一定的疑惑,这里我说下平时我们在开发 ...