详细代码如下:

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. Codeforces Round #252 (Div. 2)-C,D

    C题就是一个简单的模拟.首先给每一个人两个.然后把剩下的都给一个人就好了. 给的时候蛇形给. #include<stdio.h> #include<string.h> #inc ...

  2. .Net MVC的学习(一)

    套种间作,也挺有意思的--近来学习感悟.DRP学习的同一时候,折腾了点曾经不曾学习可是却非常多次耳闻过的东西--Asp.Net中的MVC架构模式. 一.是什么? MVC,即(Model-View-Co ...

  3. 创建一个web user control

    1.创建文件 添加,然后选择web user control 2.添加控件 在工具栏搜索button,然后拖动三个button上去 <%@ Control Language="C#&q ...

  4. xBIM 实战04 在WinForm窗体中实现IFC模型的加载与浏览

    系列目录    [已更新最新开发文章,点击查看详细]  WPF底层使用 DirectX 进行图形渲染.DirectX  能理解可由显卡直接渲染的高层元素,如纹理和渐变,所以 DirectX 效率更高. ...

  5. sql server 查询某个表一直显示"正在执行中..."的问题

    问题描述:只是单纯的执行了"select count(*) from 某表":数据表中只有一两条数据,能查询其他表,唯独这个表不能进行任何操作: 经百度搜索实验,发现应该是某个进程 ...

  6. (转载)ExpandableListView 安卓二级菜单

    ExpandableListView 安卓二级菜单   ExpandableListView可以显示一个视图垂直滚动显示两级列表中的条目,这不同于列表视图(ListView).ExpandableLi ...

  7. JavaScript学习——完善注册页面表单校验

    1.之前我们已经使用弹出框的方式实现了表单校验的功能,但是此种方式用户体验效果很差 我们希望做成把提示信息和校验结果放在输入栏的后面. 2.步骤分析 (此案例基于HTML&CSS——网站注册页 ...

  8. 虚拟机CentOS6.8下安装mycat

    安装mycat前,首先安装jdk1.7及以上版本 安装可参照 https://www.cnblogs.com/llhhll/p/9260913.html 下载mycat 1.6版本 wget   ht ...

  9. 阿里云ecs : Couldn't connect to host, port: smtp.aliyun.com, 25; timeout -1;

    上传到服务器后javamail发邮件异常 链接 原来是ECS基于安全考虑,禁用了端口25. 改成465就可以发邮件了. p.setProperty("mail.smtp.socketFact ...

  10. CodeForces-1007A Reorder the Array 贪心 田忌赛马

    题目链接:https://cn.vjudge.net/problem/CodeForces-1007A 题意 给个数组,元素的位置可以任意调换 问调换后的元素比此位置上的原元素大的元素个数最大多少 思 ...