Inverse Of Control (IOC,控制反转)
一、Inverse Of Control(IOC)的本质
1、IOC(控制反转):程序把创建对象的任务,委托第三方(spring框架)完成。
二、IOC详解
[root@rockylinux tmp]# java -version
java version "17.0.1" 2021-10-19 LTS
Java(TM) SE Runtime Environment (build 17.0.1+12-LTS-39)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.1+12-LTS-39, mixed mode, sharing)
[root@rockylinux tmp]# cat test.java
// test.java
class book{
public book(){}
public book(String ...parameters){}
public book(book obj){}
public void msg(){ System.out.println("book.msg(): hello, world!"); }
}
// core class
class spring_usr
{
public spring_usr(){}
public book create_obj( String ...para)
{
book tmp = new book(para);
return tmp;
}
}
// test functoins
public class test
{
public static void main(String[] args)
{
System.out.println("hello, java17!");
// create object normally
book b1 = new book();
// Inverse of Control (IOC), create object by SPRING frame
spring_usr sp = new spring_usr();
book b2 = sp.create_obj("hello", "hi");
b2.msg();
}
}
[root@rockylinux tmp]# javac test.java
[root@rockylinux tmp]# java test
hello, java17!
book.msg(): hello, world!
[root@rockylinux tmp]#
[root@rockylinux tmp]#
[root@rockylinux tmp]#
[root@rockylinux tmp]#
本文由 lnlidawei 原创、整理、转载,本文来自于【博客园】; 整理和转载的文章的版权归属于【原创作者】; 转载或引用时请【保留文章的来源信息】:https://www.cnblogs.com/lnlidawei/p/16052909.html