Spring 控制反转

  1. 具体内容

    Spring 开发框架之中有几个概念DI&IOC、AOP。那么要想理解Spring就必须首先理解控制反转的核心意义是什么?

    对于IOC来讲如果直接进行文字的描述,听起来会很麻烦。下面直接通过一套具体案例来分析什么IOC的核心本质。

    1. IOC分析场景

    实际上这样的操作过程就属于最初的开发阶段模式。如果换到程序的开发之中,那么可能最直白的做法就相当于是使用new来进行对象的产生。

package cn.wnh.service;

public interface IMessageService {

public String getInfo() ;

}

package cn.wnh.service.impl;

import cn.wnh.service.IMessageService;

public
class MessageServiceImpl implements IMessageService {

@Override

public String getInfo() {

return
"www.mldn.cn";

}

}

对于开发者来讲,最简单的使用形式就是直接利用关键字new实例化对象。

package cn.wnh.test;

import cn.wnh.service.IMessageService;

import cn.wnh.service.impl.MessageServiceImpl;

public
class TestMessage {

public
static
void main(String[] args) {

IMessageService msg=new MessageServiceImpl();

System.out.println(msg.getInfo());;

}

}

如果把TestMessage类比喻为一个出差员工,那么整个的处理过程中该员工必然要自己处理一切的可能出现的事务;

在整个的代码编写之中的确可以完成自己所需要描述的功能,但是会带来一个严重的问题:耦合度加深,因为在java中所有的开发里面,new是造成代码耦合度关键的元凶。

所以后来发现,如果继续这样的开发模式下去,整个代码将变为一潭死水。

这个时候应该引入一个专门负责具体操作的代理公司完成开发,这样就避免了程序员直接使用new来实例化对象了,就相当于提供了一个工厂的模型。如下图所

示:

package cn.wnh.factory;

import cn.wnh.service.IMessageService;

public
class ServiceFactory {

public
static IMessageService getInstance(String className){

IMessageService msg=null;

try{

msg=(IMessageService)Class.forName(className).newInstance();

}catch(Exception e){

e.printStackTrace();

}

return
msg;

}

}

用户只需要关注这个代理公司即可,就等于说程序员只需要传递一个完整的类名称给工厂即可。实例化交个工厂去完成。

package cn.wnh.test;

import cn.wnh.factory.ServiceFactory;

import cn.wnh.service.IMessageService;

import
cn.wnh.service.impl.MessageServiceImpl;

public
class TestMessage {

public
static
void main(String[] args) {

//IMessageService msg=new MessageServiceImpl();

IMessageService msg=ServiceFactory.getInstance("cn.wnh.service.impl.MessageImpl");

System.out.println(msg.getInfo());

}

}

通过工厂类取得了指定对象的实例,看似一切都很好,但是新的问题出现了:

所有的开发者都需要去关注工厂类;

所有的调用都必须明确的找到工厂类。

二、搭建Spring开发环境

本次将直接使用maven来进行Spring开发环境的搭建。同时本次所有的开发包都已经保存在了nexus私服上。

  1. 建立一个Mavenue的项目,mavenspring,搭建完成后需要配置与Spring有关的开发包。编辑pom.xml文件:

<dependencies>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.12</version>

</dependency>

<dependency>

<groupId>commons-logging</groupId>

<artifactId>commons-logging</artifactId>

<version>1.1.3</version>

</dependency>

<dependency>

<groupId>org.slf4j</groupId>

<artifactId>slf4j-api</artifactId>

<version>1.7.21</version>

</dependency>

<dependency>

<groupId>org.slf4j</groupId>

<artifactId>slf4j-log4j12</artifactId>

<version>1.7.21</version>

</dependency>

<dependency>

<groupId>javax</groupId>

<artifactId>javaee-api</artifactId>

<version>7.0</version>

<scope>provided</scope>

</dependency>

<dependency>

<groupId>org.glassfish.web</groupId>

<artifactId>javax.servlet.jsp.jstl</artifactId>

<version>1.2.2</version>

</dependency>

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>jstl</artifactId>

<version>1.2</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-core</artifactId>

<version>4.3.3.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-beans</artifactId>

<version>4.3.3.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-tx</artifactId>

<version>4.3.3.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>

<version>4.3.3.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context-support</artifactId>

<version>4.3.3.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-web</artifactId>

<version>4.3.3.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-webmvc</artifactId>

<version>4.3.3.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-aop</artifactId>

<version>4.3.3.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-aspects</artifactId>

<version>4.3.3.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-jdbc</artifactId>

<version>4.3.3.RELEASE</version>

</dependency>

<dependency>

<groupId>c3p0</groupId>

<artifactId>c3p0</artifactId>

<version>0.9.1.2</version>

</dependency>

<dependency>

<groupId>com.mchange</groupId>

<artifactId>mchange-commons-java</artifactId>

<version>0.2.12</version>

</dependency>

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<version>5.1.25</version>

</dependency>

</dependencies>

  1. 如果要想实现Spring的编写需要有一个核心配置文件:applicationContext.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"

xmlns:p=http://www.springframework.org/schema/p

xmlns:context="http://www.springframework.org/schema/context"

xmlns:mvc="http://www.springframework.org/schema/mvc"

xmlns:aop="http://www.springframework.org/schema/aop"

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.1.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-4.1.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-4.1.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-4.1.xsd">

</beans>

  1. 实现Spring基础配置

    在整个的开发过程之中,首先只能够通过代码来感受一下Spring基础配置,如果你需要感受到Spring完整的优点更多的学习。

    1. 之前已经实现了基础的接口和实现类,现在在此基础上修改applicationContext.xml配置文件,加入一下信息:

    <bean id="msg" class="cn.mldn.service.impl.MessageServiceImpl"/>

    1. 在测试类中编写程序代码:

      ·Spring是一个容器所以需要先模拟该容器的启动,这里面需要使用一些Spring直接的类;

      ·要通过资源文件取得IMessageService接口对象;

package cn.wnh.test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.wnh.service.IMessageService;

public
class TestMessage {

public
static
void main(String[] args) {

//IMessageService msg=new MessageServiceImpl();

//IMessageService msg=ServiceFactory.getInstance("cn.wnh.service.impl.MessageImpl");

ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

IMessageService msgService = ctx.getBean("msg",IMessageService.class) ;

System.out.println(msgService.getInfo());

}

、如果说现在使用了eclipse直接开发,但是此网络比较慢的话,这样的话程序对于xml文件的验证速度会很慢,所以为了了解这个文件,可以采用如下步骤完成:

·将所需要的xsd的文件下载下来

·建议关闭xml验证操作:在首选项中选择 "validation"配置,取消xml的验证规则:

然后将所需要的*.xsd的格式文件配置都项目里:"首选项",进入"xml"配置,添加"xml Catalog",选择"用户特定实体",然后将下载好的*.xsd文件一一添加进去,添加时请注意选择key Type 为Schema location;

为了保证正确,建议Eclipse重新启动。

4、都已经涉及到了开发框架,且开发框架以及配置好了一个常用的日志组件:log4j、slf4j。建议在 "/src/main/resources"中拷贝一个log4j.properties文件,果不配置日志文件,那么所有的错误信息都会看不到。

5、配置好了日志文件,以后进行代码测试时就可以不再使用System.out了,应该使用日志文件的形式输出;

·配置要使用的开发包,别导错了:

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

取得Logger类对象后,通过这个对象进行输出。输出日志有如下几种形式:

·info():表示普通的信息输出;

·warn():表示警告信息,但是不致命;

·error():表示出现异常信息;

·debug():表示调试信息;

通过以上的配置就可以发现在整个的Spring里面对于对象的产生完全由Spring自己来进行维护,这样的开发是有好处的,避免了关键字new。

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.wnh.service.IMessageService;

public
class TestMessage {

public
static
void main(String[] args) {

ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

IMessageService msgService = ctx.getBean("msg",IMessageService.class) ;

//IMessageService msg=new MessageServiceImpl();

//IMessageService msg=ServiceFactory.getInstance("cn.wnh.service.impl.MessageImpl");

//System.out.println(msgService.getInfo());

Logger log=LoggerFactory.getLogger(TestMessage.class);

log.info(msgService.getInfo());

log.error(msgService.getInfo());

log.warn(msgService.getInfo());

}

、既然都使用到maven开发,那么对于代码的测试就不要再去编写测试类进行测试了,应该使用junit进行测试;

在pom.xml中配置一下信息即可;

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.12</version>

</dependency>

使用junit类测试:

public
class TestMessage {

ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

@Test

public
void TestGetInfo(){

IMessageService msgservice=ctx.getBean("msg",IMessageService.class);

//输出MessageService类中getInfo()里的内容;

Logger.getLogger(TestMessage.class).info(msgservice.getInfo());

//比较MessageService类中getInfo()里的输出信息与"www.wnh.cn"进行比较",如果相等则输出;

TestCase.assertEquals(msgService.getInfo(),"www.wnh.cn");

}

总结:控制反转简单的说就是通过Spring容器的applicationContext.xml文件进行简单的配置"<bean id="msg" class="cn.mldn.service.impl.MessageServiceImpl"/>",就可以轻松实现对类的实例化,实例化名称就相当于配置文件中的id;然后便可以在测试类中取得容器对象"ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml")",通过容器对象便可以取得类对象"IMessageService msgservice=ctx.getBean("msg",IMessageService.class)"然后便可以取得类中的内容,这样的一个过程,就称为控制反转;

总的来说,Spring 的确是提供了方便的对象管理,但是其开发的过程以及所需要的开发包是非常强大的,上只是实现了一个接口对象的注入处理,以后还可能牵扯到更多的对象的注入过程。

Spring 控制反转的更多相关文章

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

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

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

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

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

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

  4. Spring控制反转(依赖注入)的最简单说明

    1.常规方式实现实例化 1.1已有角色如下: 一个接口Interface,两个接口实现类InstatnceA.InstanceB,一个调用类User 1.2当前实例化InstanceA如下: Inte ...

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

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

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

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

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

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

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

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

  9. Spring控制反转容器的使用例子

    详细代码如下: spring-config.xml <?xml version="1.0" encoding="UTF-8"?> <beans ...

随机推荐

  1. CNPM 遇到use strict的问题

    一.问题描述 [root@VM_123_144_centos node01]# cnpm install --save nodemailer /usr/lib/node_modules/cnpm/no ...

  2. Struts2 设置global timer

    设置全局的timer需要在web.xml中添加servlet, 并设置load-on-startup 为 1, 然后在servlet的init()中开启timer, 具体代码如下: 1. web.xm ...

  3. Android开发中使用static变量应该注意的问题

    package com.highxin.launcher01; import java.util.ArrayList; import java.util.HashMap; import java.ut ...

  4. 写给Android App开发人员看的Android底层知识(4)

    (八)App内部的页面跳转 在介绍完App的启动流程后,我们发现,其实就是启动一个App的首页. 接下来我们看App内部页面的跳转. 从ActivityA跳转到ActivityB,其实可以把Activ ...

  5. js文件下载及命名(兼容多浏览器)

    函数功能:实现主流浏览器的文件下载功能: 兼容性: >=IE10,Edge,chrome,firefox; 与后台的请求方式:GET请求,  url携带参数  url?id=123(隐藏文件真实 ...

  6. 同步中的四种锁synchronized、ReentrantLock、ReadWriteLock、StampedLock

    目录 1.synchronized同步锁 2.ReentrantLock重入锁 3.ReadWriteLock读写锁 4.StampedLock戳锁(目前没找到合适的名字,先这么叫吧...) 5.总结 ...

  7. 浅谈C语言指针

    下面就几种情况讨论指针. 一.指针和变量 变量是存储空间的别名,访问形式是直接访问. 指针访问内存地址是间接访问. 使用指针访问内存的场合:1.局部变量,参数传递    2.动态分配内存 指针本身也是 ...

  8. OWIN的概念初接触

    OWIN这个词我昨天才认识,一直疑惑它是个什么东西,通过一定量的研究,得到一个初步的认识,留个脚印. OWIN是什么 OWIN是一个规范和标准,旨在阐述web服务器和web应用应该如何去解耦,它使得原 ...

  9. EJB基础知识

    本人博客文章网址:https://www.peretang.com/basic-knowledge-of-ejb/ 什么是EJB 可移植的, 可重用的, 可伸缩的业务应用程序的平台 为什么选择EJB ...

  10. 动态读取文件持续显示在UI上

    private void DisplayLogInfo(FileInfo _LastFile) { if (_LastFile != null) { StreamReader sr = null; t ...