详细代码如下:

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. perl getopt 用法

    我们在linux经常常使用到一个程序须要增加參数,如今了解一下perl中的有关控制參数的函数.getopt.在linux有的參数有二种形式.一种是--help,还有一种是-h.也就是-和--的分别.- ...

  2. bzoj2663: [Beijing wc2012]灵魂宝石(二分+匈牙利)

    2663: [Beijing wc2012]灵魂宝石 题目:传送门 题解: 又是一道卡精度的题目. 很容易就可以看出单调性啊,如果R越大,选的人就越多,R越小,选的人就越少. 那最小值就直接搞咯. 那 ...

  3. apiCloud中Frame框的操作,显示与隐藏Frame

    Frame是一层一层的概念, 有的位于上层,有的位于下层. 1.加载菜单 2.加载页面层 3.首页拆分出内容层,这个时候内容层位于页面层的上方,当点击其他页面的时候,内容层遮挡住了他们 解决方案一 判 ...

  4. Devexpress控件使用二:barManager

    1.拖放控件 2.两种按钮显示形式 1)上面是大图标,下面是说明 a.Add → Largebutton 注:勾选 Show DesignTime enancements 才会出现Add b.添加图片 ...

  5. html5plus 从相册选择图片后获取图片的大小

    plus.gallery.pick(function (filePath) { plus.io.resolveLocalFileSystemURL(filePath, function (entry) ...

  6. 【参考】IBM sun.io.MalformedInputException and text encoding conversions transforms numerals to their word equivalents - United States

    Problem(Abstract) When converting contents from a file or string using WebSphere Application Server, ...

  7. swift语言点评十八-异常与错误

    1.错误类型与枚举的结合 enum VendingMachineError: Error { case invalidSelection case insufficientFunds(coinsNee ...

  8. 常用类Object,String类详解

    -------------------- String -----------------------1.求字符串长度 public int length()//返回该字符串的长度 String st ...

  9. CSS3新增的属性有哪些:

    CSS 用于控制网页的样式和布局. CSS3 是最新的 CSS 标准. CSS3新增了很多的属性,下面一起来分析一下新增的一些属性: 1.CSS3边框: border-radius:CSS3圆角边框. ...

  10. ajax的异步请求小结

    如何判断是使用json还是jsp的数据传输: json字符串可以使用js,jquery,ajax,java这几种技术,页面为jsp页面,json数据为java后台传递来. 1.同步请求可以从因特网请求 ...