java - Does using annotations to inject dependencies remove the main benefit of dependency injection(external configuration)? -


i using spring, here controller:

@controller public class personcontroller {  @resource(name="personservice") private personservice personservice;      @requestmapping(value = "/person", method = requestmethod.get)     public string getpersons(model model) {      // retrieve persons delegating call personservice     list<person> persons = personservice.getall();      // attach persons model     model.addattribute("persons", persons);     //then return view jsp        } 

and here service :

@service("personservice") @transactional public class personservice {      public list<person> getall() {         //do whatever        } } 

however, make use of di should change controller make use of interface (?) so:

@controller public class personcontroller {  @resource(name="personservice") private ipersonservice personservice; //now interface } 

this allow me, example, use 2 services 1 test , 1 live. alter adding/removing annotation on services :

@service("personservice") // line added/removed @transactional public class livepersonservice implements ipersonservice {      public list<person> getall() {         //do whatever        } } 

and

@service("personservice") //this line added/removed @transactional public class testpersonservice implements ipersonservice {      public list<person> getall() {         //do else        } } 

however 1 of main benefits lost due fact code has recompiled ? whereas if used xml lookup alter dependency on-the-fly ?

the configuration still external, because it outside define implementation going injected. inside class, hardcode "name" of something class depends on (which ok, because dependency inherent class).

this said, can use xml override annotations of code tests execution (you have specific xml application context tests) , specify implementation inject.

therefore, you don't need change code run tests. take this answer.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -