MacOS, Java和IDEAJava 杂谈

Spring中的IoC容器和依赖注入简单入门

2019-04-30  本文已影响0人  SpaceCat

Spring实际上已经快成为了java后端开发标准的一部分。其框架结构如下图。


Spring Framework

理解Spring中IoC容器和依赖注入是理解Spring的基础。

1、类之间的依赖关系

类之间的依赖(dependency)是指一个类A使用到了另一个类B,所以,有些英文的材料中将这种关系表述为引用(using)。这种关系具有偶然性、临时性,也非常弱,但是,类B的变化会影响到类A。代码中,一般表现为,类A中的某个成员函数用到了某个类B类型的参数,或者类A中的某个成员函数,用到了类B类型的某个临时变量,等等。
在UML中,依赖关系用带箭头的虚线表示,箭头从使用类指向被依赖的类。如下:


依赖关系

如下的代码展示了一个类依赖关系的例子:

// Interface HelloWorld
public interface HelloWorld {
    public void sayHello();
}
 
// Class implements HelloWorld
public class SpringHelloWorld implements HelloWorld {
   public void sayHello()  {
           System.out.println("Spring say Hello!");
   }
}
 
// Other class implements HelloWorld
public class StrutsHelloWorld implements HelloWorld {
   public void sayHello()  {
           System.out.println("Struts say Hello!");
   }
}
 
 
// And Service class
public class HelloWorldService {
    
     // Field type HelloWorld
     private HelloWorld helloWorld;
    
     // Constructor HelloWorldService
     // It initializes the values for the field 'helloWorld'
     public HelloWorldService()  {
           this.helloWorld = new StrutsHelloWorld();
     }
 
}

或者
// And Service class
public class HelloWorldService {
    
     // Field type HelloWorld
     private HelloWorld helloWorld;
    
     // Constructor HelloWorldService
     // It initializes the values for the field 'helloWorld'
     public HelloWorldService()  {
           this.helloWorld = new SpringHelloWorld();
     }
 
}

上面例子的类图如下。


示例类图

生成该类图的PlantUML脚本如下:

@startuml

Title "类图"
interface HelloWorld
HelloWorld <|.. SpringHelloWorld:实现
HelloWorld <|.. StrutsHelloWorld:实现
HelloWorldService ..> HelloWorld:依赖

@enduml

2、反转控制和依赖注入

在上面的例子中,HelloWorldService类通过HelloWorld接口依赖于StrutsHelloWorld和SpringHelloWorld类。
当HelloWorldService对象从它的构造创建时,HelloWorld对象也被创建了。它是从StrutsHelloWorld或者SpringHelloWorld创建的。
在这个例子中,要创建一个HelloWorldService对象,HelloWorld对象(SpringHelloWorld或者StrutsHelloWorld)也同时被创建。目前来看,HelloWorld对象的创建是由HelloWorldService类本身来控制的。
从这里,我们引入“IoC、控制反转”的概念。控制反转(Inversion of Control,缩写为IoC)的意思是说,我们将创建HelloWorld的控制转交给第三方。通常来说的第三方,就是指IoC容器。
IoC容器充当管理者的角色,创建HelloWorldService对象和HelloWorld对象,并通过setter方法传递HelloWorld对象到HelloWorldService。
这种,实现IoC的方式,叫做“依赖注入”。也就是说,将HelloWorldService依赖的HelloWorld对象,注入到HelloWorldService中。下面是一个示意图。


IoC和依赖注入举例

3、例子

在IDEA中创建一个空的Maven项目(需要选任何archetype),这里输入的groupid和artifactid如下。

<groupId>com.SpaceCat.SpringHelloWorld</groupId>
<artifactId>SpringHelloWorld</artifactId>
<version>1.0-SNAPSHOT</version>

然后,创建如下图的代码工程目录结构。


工程目录结构

其中,对应的代码如下。
HelloWorldProgram.java:

package com.SpaceCat.HelloWorld.spring;

import com.SpaceCat.HelloWorld.spring.helloworld.HelloWorld;
import com.SpaceCat.HelloWorld.spring.helloworld.HelloWorldService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by chengxia on 2019/4/30.
 */
public class HelloWorldProgram {
    public static void main(String[] args) {

        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        HelloWorldService service = (HelloWorldService) context.getBean("helloWorldService");
        HelloWorld hw= service.getHelloWorld();
        hw.sayHello();
    }

}

HelloWorld.java:

package com.SpaceCat.HelloWorld.spring.helloworld;

/**
 * Created by chengxia on 2019/4/30.
 */
public interface HelloWorld {
    public void sayHello();
}

HelloWorldService.java:

package com.SpaceCat.HelloWorld.spring.helloworld;

/**
 * Created by chengxia on 2019/4/30.
 */
public class HelloWorldService {
    private HelloWorld helloWorld;

    public HelloWorldService() {

    }

    public void setHelloWorld(HelloWorld helloWorld) {
        this.helloWorld = helloWorld;
    }

    public HelloWorld getHelloWorld() {
        return this.helloWorld;
    }
}

SpringHelloWorld.java:

package com.SpaceCat.HelloWorld.spring.helloworld.impl;

import com.SpaceCat.HelloWorld.spring.helloworld.HelloWorld;

/**
 * Created by chengxia on 2019/4/30.
 */
public class SpringHelloWorld implements HelloWorld {

    public void sayHello() {
        System.out.println("Spring Say Hello!!");
    }
}

StrutsHelloWorld.java:

package com.SpaceCat.HelloWorld.spring.helloworld.impl;

import com.SpaceCat.HelloWorld.spring.helloworld.HelloWorld;

/**
 * Created by chengxia on 2019/4/30.
 */
public class StrutsHelloWorld implements HelloWorld {
    public void sayHello() {
        System.out.println("Structs Say Hello!!");
    }
}

beans.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 id="springHelloWorld" class="com.SpaceCat.HelloWorld.spring.helloworld.impl.SpringHelloWorld">
    </bean>
    <bean id="strutsHelloWorld" class="com.SpaceCat.HelloWorld.spring.helloworld.impl.StrutsHelloWorld">
    </bean>

    <bean id="helloWorldService"
          class="com.SpaceCat.HelloWorld.spring.helloworld.HelloWorldService">
        <property name="helloWorld" ref="springHelloWorld"/>
    </bean>
</beans>

ClassStructure.puml(该文件只是用于绘制这个工程的类结构图,这个是用的IDEA的PlantUML插件,和工程的运行没有关系,可以不用):

@startuml

Title "类图"
interface HelloWorld
HelloWorld <|.. SpringHelloWorld:实现
HelloWorld <|.. StrutsHelloWorld:实现
HelloWorldService ..> HelloWorld:依赖

@enduml

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.SpaceCat.SpringHelloWorld</groupId>
    <artifactId>SpringHelloWorld</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!-- Spring Core -->
        <!-- http://mvnrepository.com/artifact/org.springframework/spring-core -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>
        <!-- Spring Context -->
        <!-- http://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>
    </dependencies>
</project>

完成之后,运行HelloWorldProgram.java文件,得到如下控制台输出,程序正确运行。

四月 30, 2019 7:37:02 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6267c3bb: startup date [Tue Apr 30 19:37:02 CST 2019]; root of context hierarchy
四月 30, 2019 7:37:02 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans.xml]
Spring Say Hello!!

Process finished with exit code 0

接下来,我们通过在上面的工程根目录下运行命令mvn dependency:tree来查看这个工程的用到的依赖和依赖之间的关系。如下。

$ mvn dependency:tree
[INFO] Scanning for projects...
[INFO] 
[INFO] -----------< com.SpaceCat.SpringHelloWorld:SpringHelloWorld >-----------
[INFO] Building SpringHelloWorld 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ SpringHelloWorld ---
[INFO] com.SpaceCat.SpringHelloWorld:SpringHelloWorld:jar:1.0-SNAPSHOT
[INFO] +- org.springframework:spring-core:jar:4.1.4.RELEASE:compile
[INFO] |  \- commons-logging:commons-logging:jar:1.2:compile
[INFO] \- org.springframework:spring-context:jar:4.1.4.RELEASE:compile
[INFO]    +- org.springframework:spring-aop:jar:4.1.4.RELEASE:compile
[INFO]    |  \- aopalliance:aopalliance:jar:1.0:compile
[INFO]    +- org.springframework:spring-beans:jar:4.1.4.RELEASE:compile
[INFO]    \- org.springframework:spring-expression:jar:4.1.4.RELEASE:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.427 s
[INFO] Finished at: 2019-04-30T19:39:52+08:00
[INFO] ------------------------------------------------------------------------
$ 

参考资料

上一篇 下一篇

猜你喜欢

热点阅读