Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand ioc in spring Framework

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about how to understand the ioc in the spring framework. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Abstract: the humorous explanation of ioc in spring framework IoC is Inversion of Control, control reversal. In Java development, IoC means handing over the control of your designed class to the system, not within your class. This is called a control reversal.

IoC is Inversion of Control, which reverses control. In Java development, IoC means handing over the control of your designed class to the system, not within your class. This is called control inversion.

Let's use a few examples to illustrate what IoC is.

Suppose we are going to design a Girl and a Boy class, where Girl has a kiss method, that is, Girl wants to Kiss a Boy. So, our question is, how can Girl recognize this Boy?

In China, the common ways of understanding MM and GG are as follows:

1 childhood sweetheart

2 introduction of relatives and friends

3 arranged by parents

So which one is *?

Childhood: Girl has known his Boy since childhood.

Public class Girl {void kiss () {Boy boy = new Boy ();}}

However, the disadvantage of Boy created from the beginning is that it cannot be replaced. And is responsible for the entire life cycle of Boy. What if we want to change our Girl? (the author seriously does not support frequent replacement of Boy by Girl)

Introduction of relatives and friends: the middleman is responsible for providing Boy to meet.

Public class Girl {void kiss () {Boy boy = BoyFactory.createBoy ();}}

Of course, it is good to introduce relatives and friends. If you are not satisfied, just change it for another one. However, relatives and friends BoyFactory is often in the form of Singleton, otherwise, it exists in Globals, everywhere, everywhere. It is really too cumbersome and not flexible enough. Why do I have to have this relative and friend involved? Why do we have to pay her an introduction fee? What if my friend falls in love with my boyfriend?

Parents do everything: leave everything to your parents, you don't have to make a fuss, you just need to wait for Kiss.

Http://java.chinaitlab.com/UploadFiles_8734/200701/20070123095103329.gif>; public class Girl {void kiss (Boy boy) {/ / kiss boy boy.kiss ();}}

Well, this is the way to Girl***, as long as you find a way to bribe Girl's parents and give him Boy. Then we can easily come to Kiss with Girl. It seems that thousands of years of traditional parental life is really useful. At least Boy and Girl don't have to fuss.

This is IOC, which extracts the creation and acquisition of objects externally. The required components are provided by an external container.

We know the Hollywood principle: "Do not call us, we will call you." It means You, girlie, do not call the boy. We will feed you a boy .

We should also know that we rely on the inversion principle, that is, Dependence Inversion Princinple,DIP.

Eric Gamma says programming should be oriented to abstraction. Interface-oriented programming is the core of object-oriented.

The component should be divided into two parts, namely

Service, a declaration of the functionality provided

Implementation, the realization of Service

The advantage is that multiple implementations can be switched arbitrarily to prevent "everything depends on everything" problems. That is, it depends on the specific.

Therefore, our Boy is supposed to implement the Kissable interface. So once Girl doesn't want to kiss the abominable Boy, he can also kiss the lovely kitten and the kind grandmother.

Https://cache.yisu.com/upload/information/20210521/332/454389.gif>;

2. Type of IOC

IoC's Type refers to several different ways in which Girl gets Boy. Let's explain it one by one.

IOC type 0: no IOC

Public class Girl implements Servicable {private Kissable kissable; public Girl () {kissable = new Boy ();} public void kissYourKissable () {kissable.kiss ();}}

Girl builds its own Boy, which is difficult to replace, difficult to share with others, can only be used alone, and is responsible for the full lifecycle.

IOC type 1, look at the code first:

Public class Girl implements Servicable {Kissable kissable; public void service (ServiceManager mgr) {kissable = (Kissable) mgr.lookup ("kissable");} public void kissYourKissable () {kissable.kiss ();}

This happens in Avalon Framework. If a component implements the Servicable interface, it must implement the service method and pass in a ServiceManager. It will contain other components that you need. You only need to initialize the required Boy in the service method.

In addition, getting objects from Context in J2EE also belongs to type 1.

It depends on the configuration file

...

IOC type 2:

Public class Girl {private Kissable kissable; public void setKissable (Kissable kissable) {this.kissable = kissable;} public void kissYourKissable () {kissable.kiss ();}}

Type 2 appears in Spring Framework and passes the required Boy to Girl through the set method of JavaBean. It must depend on the configuration file.

IOC type 3

Public class Girl {private Kissable kissable; public Girl (Kissable kissable) {this.kissable = kissable;} public void kissYourKissable () {kissable.kiss ();}}

This is the component of PicoContainer. Pass Boy to Girl through the constructor.

PicoContainer container = new DefaultPicoContainer (); container.registerComponentImplementation (Boy.class); container.registerComponentImplementation (Girl.class); Girl girl = (Girl) container.getComponentInstance (Girl.class); girl.kissYourKissable (). This is how the editor shares how to understand ioc in the spring framework. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report