lnlidawei

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

 

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]# 


 

posted on 2022-03-25 05:35  lnlidawei  阅读(65)  评论(0编辑  收藏  举报