How does the IOC in Java Server Spring be implemented in Java?

Source: Internet
Author: User

* * What is IOC?

The general object coupling is determined at compile time, i.e. when we write the following class:

public class Staticcoupling {

string s = new String ("hzg1981");


, class staticcoupling is coupled to the string class during compilation. In the case of code static analysis, it is possible to confirm the coupling between them.

The IOC, however, uses the Assembler object binding during runtime to bind objects that need to be coupled. The binding process is implemented by dependency injection (DI). It is important to note that there is a difference between IOC and DI. The IOC is a design paradigm (paradigm), the IOC is dependent on Di, and Di is used to implement the IOC. Dependency injection is a pattern used to create instances of objects, other objects rely on without knowing at Compil E time which class would be used to provide that functionality.

In other words, Di is a design pattern that enables IOC to be implemented. Di is not the only design pattern that can implement IOC, there are three types of three design patterns to implement the IOC design paradigm:

1 Factory mode: Factory pattern

2 Locator Mode: Service locator pattern

3 Dependency Injection mode. Dependency injection can be divided into three types: constructor injection (constructor injection) setter injection (setter injection) and pretext injection (interface injection)

* * Simple implementation of IOC

What is a di mechanism? (DI = IOC, dependence injection, inverse of control)
Dependency Injection (Dependecy injection) and control inversion (inversion of controls) are the same concept, specifically: when a role
When another role assistance is required, the caller's instance is typically created by callers during the traditional program design process. But in spring,
The work of creating the callee is no longer done by the caller, so it is called control reversal. The work of creating the callee is done by spring and then injected into the caller
Therefore also known as dependency injection.

Why is the IOC simple? In fact, it is by our usual new turn to use reflection to get the instance of the class, it is believed that anyone will use the Java reflection mechanism, it is not impossible to write an IOC framework.

Public objectgetinstance (String className) throws Exception
Object obj = Class.forName (className). newinstance ();
Method[] methods = Obj.getclass (). GetMethods ();
for (Method method:methods) {
if (Method.getname (). Intern () = = "SetString") {
Method.invoke (obj, "Hello world!");

One of the methods above is to simply use the SetString method that reflects the specified class to set a Hello world! string. You can actually see the IOC is really simple, of course, the IOC simply does not mean the spring IOC is simple, the spring IOC is powerful in a series of very powerful configuration file maintenance classes, they can maintain the spring configuration file of the relationship between the various classes, This is where the IOC of spring is really powerful. In spring's bean definition file, you can not only set properties for the definition bean, but also support inheritance between beans, bean abstraction, and different fetching methods.

* * IOC in Spring

ICO container (Container): The org.springframework.beans.factory.BeanFactory interface is the central IoC Container Interface in Spring.

Reference documents: Inversion-of-control-ioc-and-dependency-injection-di-patterns-in-spring-framework-and-related-interview-questions /


How does the IOC in Java Server Spring be implemented in Java?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.