参考:
- https://blog.csdn.net/lihao21/article/details/80803865
- http://loveruby.top/2018/02/25/Spring-Boot%E9%A1%B9%E7%9B%AEmaven%E6%89%93%E5%8C%85%E6%96%B9%E5%BC%8F%E6%80%BB%E7%BB%93/
- https://www.cnblogs.com/sxdcgaq8080/p/5650404.html
控制反转(IoC)的概念
控制反转是一种软件设计的概念。意思是在这样一种设计模式下,Java对象之间的依赖关系是由容器来控制的,而不是由对象的提供者来控制。(实现了一种反转)
控制反转可以 由多种机制实现,而依赖注入
是其中的一种实现方式。
为了理解依赖注入
,先看一下传统Java软件(不实用框架的情况下)是怎样实现对象之间依赖的。
比如我们由一个包含文本编辑器的Java程序,而在文本编辑器中有一个拼写检查的功能。
public class TextEditor {
private SpellChecker spellChecker;
public TextEditor() {
spellChecker = new SpellChecker();
}
}
先感受一下TextEditor
对象与SpellChecker
对象的依赖关系。而使用了控制反转的代码可以是这样的:
public class TextEditor {
private SpellChecker spellChecker;
public TextEditor(SpellChecker spellChecker) {
this.spellChecker = spellChecker;
}
}
可以看出,在TextEditor
对象的构造过程中,通过在构造方法中对实例变量spellChecker
进行赋值,我们创建了TextEditor对SpellChecker的依赖关系,这种依赖关系是通过构造方法注入到TextEditor对象中的。
比如我们可以通过类的使用者来实现依赖注入:
SpellChecker sc = new SpellChecker; // dependency
TextEditor textEditor = new TextEditor(sc);
而在Spring框架中,依赖注入是由Spring IoC 容器来实现的。
Spring IoC 容器
Spring IoC 容器是Spring的基础设施!
在Spring中,ApplicationContext
就是IoC容器的接口。它们负责创建Bean(对象),然后将功能类 Bean注入到你需要的Bean中。Spring框架中提供来多个该接口的实现:包括ClassPathXmlApplicationContext
,FileSystemXmlApplicationContext
,以及WebApplicationContext
。
Spring IoC 容器使用 xml 配置,或者注解配置来实现 Bean 的创建与注入。这些xml配置或者注解配置叫做元数据(就是描述数据的数据)!
比如以下方式就是通过xml配置来初始化IoC容器的方法:
ApplicationContext context =
new ClassPathXmlApplicationContext("Beans.xml");
代码
结构:
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.cqq</groupId>
<artifactId>SpringDemo-by-cqq</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>SpringDemo-by-cqq</name>
<description>Demo for Spring IoC</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<mainClass>com.cqq.MainApp</mainClass>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
TextEditor.java
package com.cqq;
public class TextEditor {
private SpellChecker spellChecker;
public TextEditor(SpellChecker spellChecker) {
System.out.println("Inside TextEditor constructor." );
this.spellChecker = spellChecker;
}
public void spellCheck() {
spellChecker.checkSpelling();
}
}
SpellChecker.java
package com.cqq;
public class SpellChecker {
public SpellChecker(){
System.out.println("Inside SpellChecker constructor." );
}
public void checkSpelling() {
System.out.println("Inside checkSpelling." );
}
}
MainApp.java
package com.cqq;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context =
new FileSystemXmlApplicationContext("file:///home/cqq/repos/SpringDemo/src/com/cqq/Beans.xml");
TextEditor te = (TextEditor) context.getBean("textEditor");
te.spellCheck();
}
}