Analysis of advantages and disadvantages of IOC in spring
This article shares the advantages and disadvantages of IOC in spring for your reference. The specific contents are as follows
1. Advantages
We know that there is a law in the basic java tutorial that tells us: all objects must be created; In other words, objects must be created before using them, but now we don't have to follow this law. We can directly obtain an object from the IOC container and use it directly without creating them in advance.
This change is just like we do not need to consider object destruction; Because Java's garbage collection mechanism helps us realize object destruction; Now there is no need to consider object creation, object creation and destruction, which has a great impact on programming.
Let's start with a simple example. There is a common class B code as follows:
There are two ways to use B:
Call method without IOC container: Bi B = New B (New a())// You need to generate instances such as a before generating instances of B. invoke();
Revolutionary calling method using IOC container: Bi B = (BI) webapputil getService(“b”); b. invoke();
There are important differences between the above two methods:
The former needs to take care of instantiation of class A in B class. If B class calls more than one class A, there are more classes like C/D/E and so on. So when you use B class, you need to study other class creation. If C/D/E is not written by yourself, you need to read its API instructions and study how they should be created. Whether to use new or factory mode or singleton call?
At this time, you will sigh: Wow, is there a mistake? I just spent so much time and energy to use a small method in class B?
When we use the second method, we don't need to spend a lot of energy and time thinking about the creation of classes such as a / C / D / E.
Using the IOC container, you don't have to do these rigid and stupid work anymore. We just grab a class from the IOC container and use them directly.
Of course, before using, we need to make a simple configuration and tell the IOC container all the classes you need to use in the future. For example, the IOC container of the jdon framework is configured as jdonframework The XML is as follows:
Note: Although class B code calls class A (or even other classes such as C), we do not need to consider this calling relationship in the configuration. Therefore, we do not need to consider the calling relationship involving other classes in class B in the whole process; This is very time-saving and labor-saving; In particular, if the project is large, there are many JavaBeans, and multiple people coordinate the development, this method can improve the development efficiency; Reducing the error rate is very helpful.
If there are many classes in your project; The call relationship is very complex, and the call relationship may change at any time. Therefore, using the IOC container without taking care of the call relationship is undoubtedly the first choice to reduce the development burden.
Another well-known implementation of IOC container is the spring framework, but in the spring configuration file ApplicationContext In XML, we must consider the above call relationship:
2. Disadvantages
What is the biggest disadvantage of IOC?
The steps of generating an object become more complex (in fact, the operation is quite simple). For those who are not used to this method, they will feel a little awkward and intuitive. Because object generation uses reflection programming, there is some loss in efficiency. However, compared with the improved maintainability and flexibility of IOC, this loss is insignificant, unless the generation of an object requires high efficiency.
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.