dependency injection - Spring How its helpful -


this question has answer here:

i have class

@component public class messageservicehelper {     @autowired     private messageservice messageservice;      public boolean sendmessage(string text){         return messageservice.sendmessage(text);     }     public messageservice getmessageservice() {         return messageservice;     }      public void setmessageservice(messageservice messageservice) {         this.messageservice = messageservice;     } } 

so here autowiring messageservice, whenever object ob messageservicehelper instantiated automatically inject dependency messageservice messageservicehelper. same thing can achieve if have write other class create instance of messageservice , call setter method.

now here point can raised have shifted dependency resolution logic else , code coupled instantiation of messageservice , if implementation changed have change class spring if have change implementation have make change in metadata have given before.

so here question different di? strongest point di here?

in short, metadata change desirable enables configuration of higher layer of abstraction rather change in application code. configurable higher layer reusable , lower layer of application code testable. dependency injection 1 of old core design principles featuring in famous solid design principles. design principle has featured lots of frameworks out there - spring being 1 of popular , evangelist of di in java world. there lots of articles available understand necessity of di- can read them on wikipedia , microsoft library. , yes, there few gem of articles on martin fowler's site understand in depth here , here.


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -