spring - Calling service layer directly from main class -


i have project structure controller -> response builder -> service layer. here service layer call repository layer(database layer).

everything ok when follow structure.

but,for testing want call service layer directly java main class.

how can this??????

my controller:

@restcontroller @requestmapping("/ishmam") public class ishmamaddresscontroller {      @autowired     @qualifier("ishmamaddressbuilder")     ishmamaddressbuilder ishmamaddressbuilder;       @requestmapping("/getaddress")     public iresponsedto<webbcustomeraddressdto> getalladdress(){         return ishmamaddressbuilder.getalladdress();     }  } 

my builder class is:

@component("ishmamaddressbuilder") public class ishmamaddressbuilder {      @autowired     @qualifier("ishmamaddressserviceimpl")     ishmamaddressinterface ishmamaddressservice;      public iresponsedto<ishmamaddressresponsedto> getalladdress(){     iresponsedto<webbcustomeraddressdto> response=new                 iresponsedto<webbcustomeraddressdto>();      try{         //here call service layer         list<string> addresslist=ishmamaddressservice.getalladdress();      }catch(exception e){         throw e;     }     return addresslist;  } 

my service layer is:

@service("ishmamaddressserviceimpl") @transactional  public class ishmamaddressserviceimpl implements ishmamaddressinterface {      @autowired(required = true)     @qualifier("webcustomeraddressrepository")     webcustomeraddressrepository webcustomeraddressrepository;      @override     public ishmamaddressresponsedto getalladdress() {         list<webcustomeraddress> alist = new arraylist<webcustomeraddress>();         list<webbcustomeraddressdto> dtowebcustomeraddresslist = new                        arraylist<webbcustomeraddressdto>();          ishmamaddressresponsedto ishmamaddressresponsedto=new                        ishmamaddressresponsedto();          try{              alist =              address.getalladdress(1);//calling database layer               ishmamaddressresponsedto=//doing something,not important                                         //question     }      return ishmamaddressresponsedto;  }            

now want call service layer directly main class:

public class address{       public void getaddress(){            ishmamaddressserviceimpl i=new ishmamaddressserviceimpl();            list<string> list=i.getalladdress();      }        public static void main(string[] args){            address a=new address();            a.getaddress();       }  } 

this process not working.how can this???????

as use spring, must never use new build object should managed spring.

so could:

  • either manually, means bootstrap application context same intialization have in application , explicitely bean it

    applicationcontext ctx = new annotationconfigapplicationcontext(     class<?>... annotatedclasses); // if way use ... ishmamaddressinterface = ctx.getbean("ishmamaddressserviceimpl",      ishmamaddressinterface .class); 
  • or use spring test framework automatically in junit tests

    @webappconfiguration @contextconfiguration(classes = ...) public class webintegrationtests {      @autowired     @qualifier("ishmamaddressserviceimpl")     ishmamaddressinterface ishmamaddressservice;     ...     @test     public void testgetaddress() {         ishmamaddressservice.getaddress();         ...     } } 

Comments

Popular posts from this blog

gridview - Yii2 DataPorivider $totalSum for a column -

java - Suppress Jboss version details from HTTP error response -

Sass watch command compiles .scss files before full sftp upload -