简单实现 IoC

IoC 控制反转

  1. 新建一个Spring Starter Project

  2. 定义接口 USB,包含3个方法

/**
 * USB设备
 */
public interface Usb {
	
	/**
	 * 挂载
	 */
	void load();
	
	/**
	 * 工作
	 */
	void work();
	
	/**
	 * 卸载
	 */
	void unload();
}

  1. 定义类实现 USB 接口:鼠标、键盘、U盘、电源适配器…,这里只展示实现类鼠标,其他实现类同理,记得加上注解 @Component 声明为组件,需要延时加载的话可以加上注解 @Lazy
package com.newer.ioc;

import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

@Component("mouse")
@Lazy
public class Mouse implements Usb{
	
	public Mouse() {
		System.out.println("Mouse() 构造方法");
	}

	@Override
	public void load() {
		System.out.println("挂载 mouse");
		
	}

	@Override
	public void work() {
		System.out.println("mouse 工作中...");
		
	}

	@Override
	public void unload() {
		System.out.println("卸载 mouse");
		
	}

}

  1. 定义类 PC
import java.util.ArrayList;
import java.util.Arrays;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

@Component
@Lazy
public class Pc {
	
	// 开销小,轻量级
	Usb[] usbList;

	// 自动加载 指定加载类型 延时加载
	@Autowired
	@Qualifier("mouse")
	@Lazy
	Usb usb1;
	
	@Autowired
	@Qualifier("keyboard")
	@Lazy
	Usb usb2;
	
	@Autowired
	@Qualifier("flashdisk")
	@Lazy
	Usb usb3;
	
	@Autowired
	@Qualifier("poweradapter")
	@Lazy
	Usb usb4;
	
	public Pc() {
		
		System.out.println("Pc() 构造方法");
		usbList = new Usb[4];
		System.out.println(Arrays.toString(usbList));
	}
	
	// PC被创建后自动调用,挂载 USB 设备
	@PostConstruct
	public void start() {
		usbList[0] = usb1;
		usbList[1] = usb2;
		usbList[2] = usb3;
		usbList[3] = usb4;

		for (Usb usb : usbList) {
			if(usb!=null) {
				usb.load();
			}
		}
		
	}
	
	public void run() {
	
		for (Usb usb : usbList) {
			if(usb!=null) {
				usb.work();
			}
		}
		
	}
	
	// PC销毁时自动调用,卸载设备
	@PreDestroy
	public void stop() {
		for (Usb usb : usbList) {
			if(usb!=null) {
				usb.unload();
			}
		}
	}
	
}

  1. Spring Main 运行,记得加上注解 @Bean 才会被调用。
import org.springframework.beans.factory.BeanFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class IocHwApplication {

	public static void main(String[] args) {
		SpringApplication.run(IocHwApplication.class, args);
	}

	// BeanFactory factory 组件工厂 
	@Bean
	public CommandLineRunner usb(BeanFactory factory) {
		return new CommandLineRunner() {
			@Override
			public void run(String... args) throws Exception {
				// 从组件工厂获得 PC 实例
				// 基于反射机制,根据传入类型,返回一个实例
				Pc pc = factory.getBean(Pc.class);
				pc.run();
			}
		};
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值