轻松理解spring IOC
spring IOC(Inversion of control)即控制反转
概念:一,spring框架的核心之一
二,控制权由对象本身转向容器;由容器根据配置文件去创建实例并创建各个实例之间的依赖关系
接下来我们以一个数据库连接的案列来阐述IOC的工作原理,下图为该项目的结构体系

本例中我们着重关注util(获取数据库连接对象)以及dao(数据库访问层)的依赖关系解析
本例需求:dao层可以自由,简洁操作mysql和sqlserver数据库
第一步:我们先定义了获取连接对象的接口
package util;
import java.sql.Connection;
public interface MyConnection {
public Connection getConnection();
}
第二步:mysql和sqlserver对上一接口的具体实现(以sqlserver为例)
package util; import java.sql.Connection;
import java.sql.DriverManager; public class MyConnection_sqlserver implements MyConnection {
// 第一步:获取数据连接,让appliction server能够与db server进行交互
private String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private String url = "jdbc:sqlserver://localhost:1433;DatabaseName=CardDB";
private String name = "sa";
private String pwd = "123456";
private Connection conn = null; @Override
public Connection getConnection() {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
System.out.println( e.getMessage() );
}
try{
conn = DriverManager.getConnection(url, name, pwd);
}
catch(Exception e){
System.out.println("获取数据库连接时有异常:"+e.getMessage());
}
return conn;
} }
第三步:dao获取连接对象
package dao; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List; import entity.BookCard;
import util.MyConnection; public class BookCardDaoImpl implements BookCardDao { private MyConnection c; public void setC(MyConnection c) {
this.c = c;
}
解析:一,在dao中我们只定义了一个连接对象的属性,我们并不用知道具体是哪一个数据库的连接,只需要利用这个对象进行数据库操作即可
二,需要在本类中提供一个公共的set方法以便spring将这个连接对象注入进来
我们可以对照面向对象编程的写法来进一步了解IOC的长处
public class BookCardDaoImpl implements BookCardDao {
MyConnection c = new MyConnection_sqlserver();
解析:在传统面向对象的编程中我们层层级之间的关系紧密耦合在一起这就可能会引起这样一个问题,如果某一层出现问题,则可能影响到其他层,所以迫使其它层也需要作出调整
关键--》spring注入
<?xml version="1.0" encoding="UTF-8"?>
<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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd"> <bean id="myDao1" class="dao.BookCardDaoImpl" scope="prototype">
<property name="c" ref="myConnection1"></property>
</bean> <bean id="myConnection1" class="util.MyConnection_mysql" scope="prototype"></bean> <bean id="myConnection2" class="util.MyConnection_sqlserver" scope="prototype"></bean> </beans>
解析:一,以上为部分spring的配置文件
二,回到本例的需求上来分析,如果我们需要操作mysql数据库只需要在dao这个bean中引用bean_id为myConnection1的bean,同理SqlServer则引用myConnection2
本篇博客到此结束,如果想继续深入理解IOC的朋友欢迎大家阅读下篇自己动手写spring IOC框架
轻松理解spring IOC的更多相关文章
- Java工厂模式解耦 —— 理解Spring IOC
Java工厂模式解耦 -- 理解Spring IOC 最近看到一个很好的思想来理解Spring IOC,故记录下来. 资源获取方式 主动式:(要什么资源都自己创建) 被动式:(资源的获取不是我们创建, ...
- 轻松理解 Spring AOP
目录 Spring AOP 简介 Spring AOP 的基本概念 面向切面编程 AOP 的目的 AOP 术语和流程 术语 流程 五大通知执行顺序 例子 图例 实际的代码 使用 Spring AOP ...
- 如何理解Spring IOC
Spring IOC 思维导图 要了解控制反转( Inversion of Control ), 我觉得有必要先了解软件设计的一个重要思想:依赖倒置原则(Dependency Inversion Pr ...
- 【死磕 Spring】----- IOC 之深入理解 Spring IoC
在一开始学习 Spring 的时候,我们就接触 IoC 了,作为 Spring 第一个最核心的概念,我们在解读它源码之前一定需要对其有深入的认识,本篇为[死磕 Spring]系列博客的第一篇博文,主要 ...
- IOC 之深入理解 Spring IoC
在一开始学习 Spring 的时候,我们就接触 IoC 了,作为 Spring 第一个最核心的概念,我们在解读它源码之前一定需要对其有深入的认识,本篇为[死磕 Spring]系列博客的第一篇博文,主要 ...
- 深入理解Spring IOC
转载自 http://www.cnblogs.com/xdp-gacl/p/4249939.html 学习过Spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概 ...
- 理解 Spring IoC 容器
控制反转与大家熟知的依赖注入同理, 这是通过依赖注入对象的过程. 创建 Bean 后, 依赖的对象由控制反转容器通过构造参数 工厂方法参数或者属性注入. 创建过程相对于普通创建对象的过程是反向, 称之 ...
- 深入理解Spring IOC工作原理
为什么会出现spring,spring出现解决了什么问题? 1.分析普通多层架构存在的问题 JSP->Servlet->Service->Dao 层与层之间的依赖很强,属于耦合而且是 ...
- 深入理解Spring IoC容器和动态代理机制
Deployment期间验证 实现一: <bean id="theTargetBean" class="..."/> <bean id=&qu ...
随机推荐
- 【转】Java并发编程:深入剖析ThreadLocal
来自: http://www.importnew.com/17849.html 想必很多朋友对ThreadLocal并不陌生,今天我们就来一起探讨下ThreadLocal的使用方法和实现原理.首先,本 ...
- C++的友元类和友元函数实例
#include <math.h> #include<iostream> using namespace std; class Point { public: Point(do ...
- js鼠标滚轮事件
不多说,直接上代码. //非ie document.body.onmousewheel = function(event) { event = event || window.event; conso ...
- Android Studio failed to open by giving error “Files Locked” 解决方案
windows 7 下的解决方案 导航至 android-studio 安装目录. (默认为C:\Program Files (x86)\Android\android-studio). 往上一层文件 ...
- [WPF系列]-Data Validation
项目经常前台界面涉及到用户输入时,我们常常会用到数据有效性的验证.在网页中我们之前用js来校验Form中的数据有效性.在WPF中我们如何实现这种验证机制了?答案:INotifyDataErrorInf ...
- ClearContainer 网络部分源码分析
// cc-oci-runtime/src/oci.c /*! * Create the state file, apply mounts and run hooks, but do not star ...
- python 类属性与方法
Python 类属性与方法 标签(空格分隔): Python Python的访问限制 Python支持面向对象,其对属性的权限控制通过属性名来实现,如果一个属性有双下划线开头(__),该属性就无法被外 ...
- 深入理解Java之泛型
原文出处: absfree 1. Why ——引入泛型机制的原因 假如我们想要实现一个String数组,并且要求它可以动态改变大小,这时我们都会想到用ArrayList来聚合String对象.然而,过 ...
- Apache Shiro系列之五,概述 —— 配置
Shiro设计的初衷就是可以运行于任何环境:无论是简单的命令行应用程序还是复杂的企业集群应用.由于运行环境的多样性,所以有多种配置机制可用于配置,本节我们将介绍Shiro内核支持的这几种配置机制. ...
- asp.net mvc ajax 异步刷新例子
这几天在asp.net中使用ajax来做异步刷新,这里整理一下 1.首先看前台页面点击的时候调用函数 function shuxin() { $.ajax( { url: "GetValue ...