IoC概述
IoC(Inverse of Control)控制反轉,是一種設計思想。依賴對象的建立由IoC容器建立并注入到被注入對象中。這時不需要被注入對象不需要管理依賴對象,隻需要被動使用即可。
控制什麼?Java開發中,對象A依賴對象B,一般都是對象A之間建立對象B,是由對象A主動建立的。而IoC會專門負責依賴對象的建立,由IoC容器控制外部資源的擷取。
反轉了什麼?用戶端依賴的對象由自己主動建立變成容器建立和裝配依賴對象,依賴對象的擷取被反轉。
IoC分類
DL依賴查找
不常用的方式,使用API自己查找資源群組裝對象。
DI依賴注入
常用方式,主要分為接口注入,構造器注入和setter注入。
接口注入
接口注入需要實作一個接口,接口提供一個方法,将依賴對象注入進去。
// 汽車類,依賴對象
public class Car {
public void driver(){
System.out.println("The car is running!");
}
}
//要實作的接口
public interface CarInterface {
void injectCar(Car car);
}
//被注入的對象,需要實作接口,實作注入方法
public class Man implements CarInterface{
private Car car;
@Override
public void injectCar(Car car) {
this.car = car;
}
public void driver(){
car.driver();
}
}
構造器注入
// 汽車類,依賴對象
public class Car {
public void driver(){
System.out.println("The car is running!");
}
}
//構造器注入必須提供一個構造器
public class Unknow {
private Car car;
public Unknow(Car car){
this.car = car;
}
public void driver(){
car.driver();
}
}
setter注入
// 汽車類,依賴對象
public class Car {
public void driver(){
System.out.println("The car is running!");
}
}
//提供setter方法注入對象
public class Woman {
private Car car;
public void driver(){
car.driver();
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
}
Spring的DI
Spirng中支援構造器注入和setter注入。
主要由兩類IoC容器:BeanFactory和ApplicationContext。
XML配置方式
構造器注入
配置檔案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 class="com.tetris.Car" id="car"/>
<bean class="com.tetris.Man" id="man">
<constructor-arg name="car" ref="car"/>
</bean>
</beans>
代碼
// 汽車類,依賴對象
public class Car {
public void driver(){
System.out.println("The car is running!");
}
}
public class Man {
private Car car;
public Man(Car car){
this.car = car;
}
public void driver(){
car.run();
}
}
public class App
{
public static void main( String[] args )
{
ApplicationContext context = new FileSystemXmlApplicationContext("classpath:beans.xml");
Man man = (Man) context.getBean("man");
man.driver();
}
}
setter注入
配置檔案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 class="com.tetris.Car" id="car"/>
<bean class="com.tetris.Man" id="man">
<property name="car" ref="car"/>
</bean>
</beans>
Java編碼
// 汽車類,依賴對象
public class Car {
public void driver(){
System.out.println("The car is running!");
}
}
//提供setter方法注入對象
public class Woman {
private Car car;
public void driver(){
car.driver();
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
}
public class App
{
public static void main( String[] args )
{
ApplicationContext context = new FileSystemXmlApplicationContext("classpath:beans.xml");
Man man = (Man) context.getBean("man");
man.driver();
}
}
注解配置方式
構造器注入
配置檔案,開啟注解掃描
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.tetris"/>
</beans>
Java編碼
//使用注解
@Component
public class Car {
public void run(){
System.out.println("The car is runnig!");
}
}
//
@Component
public class Man {
private Car car;
@Autowired
public Man(Car car) {
this.car = car;
}
public void driver() {
car.run();
}
}
//
public class App
{
public static void main( String[] args )
{
ApplicationContext context = new FileSystemXmlApplicationContext("classpath:beans.xml");
Man man = (Man) context.getBean("man");
man.driver();
}
}
setter注入
配置檔案,開啟注解掃描
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.tetris"/>
</beans>
Java編碼
//使用注解
@Component
public class Car {
public void run(){
System.out.println("The car is runnig!");
}
}
@Component
public class Man {
private Car car;
public void driver() {
car.run();
}
public Car getCar() {
return car;
}
@Autowired
public void setCar(Car car) {
this.car = car;
}
}
//
public class App
{
public static void main( String[] args )
{
ApplicationContext context = new FileSystemXmlApplicationContext("classpath:beans.xml");
Man man = (Man) context.getBean("man");
man.driver();
}
}
@Autowired可以用在類成員變量,構造器和方法上。
預設是使用byType注入,有且隻能有一個依賴對象,否則會報錯,可以使用required設定為false;如果要使用byName注入,可以使用@Qualifier注解配合使用。