详细代码如下:

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. python设计模式 之 简单工厂模式

    简单工厂模式属于类的创建型模式,适合用来对大量具有共同接口的类进行实例化,它能够推迟到执行的时候才动态决定要创建哪个类的实例.而不是在编译时就必须知道要实例化哪个类. python: #!/usr/b ...

  2. easyui编辑器(kindeditor-4.1.10)

    //1  重写kindedit    -建一个js文件 easyui_kindeditor.js (function ($, K) {     if (!K)         throw " ...

  3. Maximum Bipartite Matching

    算法旨在用尽可能简单的思路解决这个问题.理解算法也应该是一个越看越简单的过程,当你看到算法里的一串概念,或者一大坨代码,第一感觉是复杂,此时最好还是从样例入手.通过一个简单的样例,并编程实现,这个过程 ...

  4. C++ STL之list具体解释

    list容器是一个双向链表,能够高效地进行插入删除元素. 构造函数 list<Elem> c;//空list list<int> c(3);//创建一个含有三个默认值是0的元素 ...

  5. [luogu P4197] Peaks 解题报告(在线:kruskal重构树+主席树 离线:主席树+线段树合并)

    题目链接: https://www.luogu.org/problemnew/show/P4197 题目: 在Bytemountains有N座山峰,每座山峰有他的高度$h_i$.有些山峰之间有双向道路 ...

  6. xBIM 基础14 使用LINQ实现最佳性能(优化查询)

    系列目录    [已更新最新开发文章,点击查看详细]  LINQ代表语言集成查询,它是3.5版以来的.NET Framework的一部分.它实现延迟执行,这意味着您可以链接查询语句,并且在您实际迭代结 ...

  7. POJ 3178 凸包+DP (巨坑)

    题意: 思路: 这题巨坑!!! 这题巨坑!!! 这题巨坑!!! 这题巨坑!!! 这题巨坑!!! (而且没有题解--.5555555--) 只能照着自己想的写了-- 先求出来凸包 求凸包的方法呢:先找出 ...

  8. 三个获取浏览器URL中参数值的方法

    这三个是一般的获取浏览器传的参数值的方法,之前有用unescape()解码的方法,但是遇到汉字会产生乱码,所以用decodeURI(); 方法一: function getQueryString(na ...

  9. Java7里try-with-resources分析--转

    原文地址:http://blog.csdn.net/hengyunabc/article/details/18459463 这个所谓的try-with-resources,是个语法糖.实际上就是自动调 ...

  10. js小知识colspan和rowspan

    colspan和rowspan这两个属性用于合并表格的列或者行. colspan是"column  span"(跨列)的缩写,所以colspan属性用在td标签中,用来跨列合并单元 ...