详细代码如下:

spring-config.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"> <bean name="product" class="com.mstf.bean.Product"/> <!-- 通过参数名传递参数 -->
<bean name="featuredProduct" class="com.mstf.bean.Product">
<constructor-arg name="name" value="孝感"/>
<constructor-arg name="description" value="简介"/>
<constructor-arg name="price" value="9.95"/>
</bean> <!-- 通过指数方式传递参数 -->
<bean name="featuredProduct2" class="com.mstf.bean.Product">
<constructor-arg index="0" value="孝感"/>
<constructor-arg index="1" value="简介"/>
<constructor-arg index="2" value="9.95"/>
</bean> <bean id="calendar" class="java.util.Calendar" factory-method="getInstance"/> <!-- 通过构造器方式实例化 -->
<bean name="employee1" class="com.mstf.bean.Employee">
<property name="homeAddress" ref="simpleAddress"/>
<property name="firstName" value="孝"/>
<property name="lastName" value="感"/>
</bean> <!-- 构造器方式依赖注入 -->
<bean name="employee2" class="com.mstf.bean.Employee">
<constructor-arg name="firstName" value="孝"/>
<constructor-arg name="lastName" value="感"/>
<constructor-arg name="homeAddress" ref="simpleAddress"/>
</bean> <!-- 被依赖类(Employee依赖于Address类,以下配置是为了每个Employee实例都能包含Address实例) -->
<bean name="simpleAddress" class="com.mstf.bean.Address">
<constructor-arg name="line1" value="湖北省孝感市"/>
<constructor-arg name="line2" value=""/>
<constructor-arg name="city" value="孝感市"/>
<constructor-arg name="state" value="China"/>
<constructor-arg name="zipCode" value="99999"/>
<constructor-arg name="country" value="CN"/>
</bean> </beans>

  Address

package com.mstf.bean;
/**
* Address类和Employee类属于setter方式依赖注入
* @author wangzheng
*
*/
public class Address {
private String line1;
private String line2;
private String city;
private String state;
private String zipCode;
private String country; public Address(String line1, String line2, String city, String state, String zipCode, String country) {
super();
this.line1 = line1;
this.line2 = line2;
this.city = city;
this.state = state;
this.zipCode = zipCode;
this.country = country;
} @Override
public String toString() {
return "Address [line1=" + line1 + ", line2=" + line2 + ", city=" + city + ", state=" + state + ", zipCode="
+ zipCode + ", country=" + country + "]";
} }

  Employee

package com.mstf.bean;
/**
* Employee类和Address类属于setter方式依赖注入
* @author wangzheng
*
*/
public class Employee {
private String firstName;
private String lastName;
private Address homeAddress; public Employee() { } public Employee(String firstName, String lastName, Address homeAddress) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.homeAddress = homeAddress;
} public String getFirstName() {
return firstName;
} public void setFirstName(String firstName) {
this.firstName = firstName;
} public String getLastName() {
return lastName;
} public void setLastName(String lastName) {
this.lastName = lastName;
} public Address getHomeAddress() {
return homeAddress;
} public void setHomeAddress(Address homeAddress) {
this.homeAddress = homeAddress;
} @Override
public String toString() {
return "Employee [firstName=" + firstName + ", lastName=" + lastName + ", homeAddress=" + homeAddress + "]";
} }

  Product

package com.mstf.bean;

import java.io.Serializable;
/**
* 带参数的构造器来初始化类
* @author wangzheng
*
*/
public class Product implements Serializable { // product类 private static final long serialVersionUID = 1L; private String name;
private String descripition;
private float price; public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescripition() {
return descripition;
}
public void setDescripition(String descripition) {
this.descripition = descripition;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
} public Product() { } public Product(String name, String descripition, float price) {
super();
this.name = name;
this.descripition = descripition;
this.price = price;
} }

  

Spring控制反转容器的使用例子的更多相关文章

  1. Spring 控制反转容器(Inversion of Control – IOC)

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

  2. PHP关于依赖注入(控制反转)的解释和例子说明

    PHP关于依赖注入(控制反转)的解释和例子说明 发表于2年前(2014-03-20 10:12)   阅读(726) | 评论(1) 8人收藏此文章, 我要收藏 赞2 阿里云双11绽放在即 1111 ...

  3. Spring 控制反转

    Spring 控制反转 具体内容 Spring 开发框架之中有几个概念DI&IOC.AOP.那么要想理解Spring就必须首先理解控制反转的核心意义是什么? 对于IOC来讲如果直接进行文字的描 ...

  4. Spring控制反转(IOC)和依赖注入(DI),再记不住就去出家!

    每次看完spring的东西感觉都理解了,但是过了一段时间就忘,可能是不常用吧,也是没理解好,这次记下来. 拿ssh框架中的action,service,dao这三层举例: 控制反转:完成一个更新用户信 ...

  5. Spring控制反转与依赖注入(IOC、DI)

    IOC: 反转控制   Inverse Of Control DI:依赖注入 Dependency Injection 目的:完成程序的解耦合 解释:在应用系统的开发过程中,有spring负责对象的创 ...

  6. spring 控制反转与依赖注入原理-学习笔记

    在Spring中有两个非常重要的概念,控制反转和依赖注入:控制反转将依赖对象的创建和管理交由Spring容器,而依赖注入则是在控制反转的基础上将Spring容器管理的依赖对象注入到应用之中: 所谓依赖 ...

  7. 对spring控制反转以及依赖注入的理解

    一.说到依赖注入(控制反转),先要理解什么是依赖. Spring 把相互协作的关系称为依赖关系.假如 A组件调用了 B组件的方法,我们可称A组件依赖于 B组件. 二.什么是依赖注入. 在传统的程序设计 ...

  8. Spring 控制反转和依赖注入

    控制反转的类型 控制反转(IOC)旨在提供一种更简单的机制,来设置组件的依赖项,并在整个生命周期管理这些依赖项.通常,控制反转可以分成两种子类型:依赖注入(DI)和依赖查找(DL),这些子类型各自又可 ...

  9. 尚学堂Spring视频教程(二):Spring控制反转

    用Spring来实现IOC 在上节中我们自定义了一个接口BeanFactory和类ClassPathXmlApplicationContext来模拟Spring,其实它们在Spring中确实是存在的, ...

随机推荐

  1. android drawable资源调用使用心得

    1. 调用顺序 android 调用应用图片资源时,会优先选择当前手机屏幕dpi对应的的文件夹(如drawable-ldpi, drawable-mdpi, drawable-hdpi, drawab ...

  2. 安卓通过OkHttp获取json数据

    使用Http协议访问网络 OkHttp使用 可以很好的获取接口数据!json数据! 支持get和post提交方式!!! 1.引入模块 compile 'com.squareup.okhttp3:okh ...

  3. elasticsearch index 之 put mapping

    elasticsearch index 之 put mapping   mapping机制使得elasticsearch索引数据变的更加灵活,近乎于no schema.mapping可以在建立索引时设 ...

  4. ionic2集成极光推送

    ionic2集成极光推送: ionic2api:https://ionicframework.com/docs/ 极光推送官网:https://www.jiguang.cn android-怎么注册极 ...

  5. 寻找两个有序数组的中位数 C++实现leetcode系列(四)

    给定两个大小为 m 和 n 的有序数组 nums1和 nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 你可以假设 nums1 和 nums2 不 ...

  6. 移动端fixed后 横竖屏切换时上部或下部出现空隙问题

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. sql server 更新满足条件的某一条记录

    上图数据:SNum为”18004XXXXX000001K2GW 4000 L1C“,OffLineStation为“OP1010”的有两条数据,当where条件中为上述两者时会同时更新这两条数据,并不 ...

  8. Core Java(一)

    一. 绪 1.软件:按照特定顺序组织的计算机数据和指令的集合. 软件开发:借助开发工具与计算机语言制作软件 2.计算机语言:用于人与计算机通讯的语言 分为机器语言,汇编语言,高级语言 高级语言分为编译 ...

  9. 用latex写简历

    最近终于逐渐掌握了用Latex写中文简历的问题.首先就是必须基于交大前辈bin yuan的模板进行学习. 如果有了一般论文撰写和Beamer做PPT的经验,不难发现resume的模板也是遵循一套程式的 ...

  10. tabIndex-bootstrap中Get到的

    网页键盘的无障碍访问性 其实加了这个,可以控制Tab键切换的顺序,聚焦等 这个属性,任何标签都可以添加,没有兼容性限制,属性值的范围:0-32767 当一个元素设置tabindex属性值为-1的时候, ...