Home > Back-end >  Spring IOC and the advantage of DI?
Spring IOC and the advantage of DI?

Time:01-31

IOC small white thought is originally need to his new class to the IOC factory to deal with the new class this



DI is originally need our own new class can now through dependency injection


Dependency injection need to write one line of code, the new new class also need to write one line of code advantage in where?


Someone said that the need to change the constructor instead that I new one directly without no-parameter constructor? Thought it seemed to have no advantage to say advantage isn't in that factory pattern mode when not hungry sweat is don't need a new one advantage?


Question from small white? Request a great god to solve it

CodePudding user response:

The landlord did not understand what is IOC, the IOC is a programming thought, is not the kind of technology, specific SpringIOC is to use the bean container to manage to create a good example, these beans is waived by default, and DI, is the instance objects assigned to reference variables, so that we can solve, circular dependencies between objects for achieving the object's reusability,

CodePudding user response:

Simplify the code,,,

CodePudding user response:

Changes have a lot of good changes when your project to find a new when the time comes to modify

CodePudding user response:

Spring to help you manage those instances, and default is the singleton pattern

CodePudding user response:

The function entry (such as: web request, timing task) to bind to the spring on the management method of bean;
The entrance based bean bean rely on other, are kept in the spring to scan, initialization, injection;
Bean depends on the basis of configuration items also get and injection by spring,

Based bean is responsible for operating the database, for example, if a mixed start to the spring dependency injection work completely, there may be outsourced to write such a highly coupled code below:
Entrance based bean bean must be in the new one, after first based bean init method to the incoming call database configuration, and then based bean update method can run normally; , in turn, the same new, init, a link to update three code appears in a number of different entrance bean; Even the entrance to the beans from different configuration file read the address of the same database configuration,

CodePudding user response:

The most simple example, you use the MySql today, tomorrow want to Oracle, the day after tomorrow is going to change essentially,
Standard practice is to change the code every day, today the new MySql (); Tomorrow to the new Oracle ();

Don't have to change if use injection method, the source code, a new implementation of Oracle, and then modify the configuration can be released,

CodePudding user response:

The above example database, might not be appropriate, after all every day few items in the database,

You change the database to the business to understand, such as goods, price of 10 yuan today, tomorrow May 8 discount, after to continue to 9 discount, then change to the original price but send towel,
You don't have to do injection, would get into every day to modify the vortex of the old code,

CodePudding user response:

reference 7 floor water 2 reply:
the above example database, might not be appropriate, after all, few projects every day in the database,

You change the database to the business to understand, such as goods, price of 10 yuan today, tomorrow May 8 discount, after to continue to 9 discount, then change to the original price but send towel,
You don't have to do injection, would get into every day to modify the vortex of the old code,

Feel this is the category of oop, should use design patterns to solve,
DI can reduce duplication of new code + statement cycle,
  • Related