how to use @Service annotation in Spring MVC to create the bean of Service layer -


can tell how bean of service layer in spring mvc. 1 way bean of service layer using @service annotation how that, don't know. controller:

@controller public class configureapplicationcontroller {      @requestmapping(value="/configureapplication.html", method=requestmethod.get)     public modelandview getlistofallconfigureapplication(){          appconfigureserviceimpl getservice=new appconfigureserviceimpl();          arraylist<configureapplication> results =getservice.getlistofallappconfigure();         modelandview model=new modelandview("configureapplication");         model.addobject("results",results);         return model;     } 

and serviceimpl is:

@service("appconfigureserviceimpl") public class appconfigureserviceimpl implements appconfigureservice {      public arraylist<configureapplication> getlistofallappconfigure(){          @suppresswarnings("resource")         applicationcontext ctx=new classpathxmlapplicationcontext("spring-dispatcher-servlet.xml");         appconfiguredaoimpl getall=ctx.getbean("appconfiguredaoimpl", appconfiguredaoimpl.class);          arraylist<configureapplication> results =getall.getlist();         return results;     } 

in have made object of appconfigureserviceimpl (in service layer)then invoke method doing not using dependency injection in spring. know can using @service annotation don';t know syntax. can me solve problem.

put plain @service annotation on appconfigureserviceimpl (you don't have specify "appconfigureserviceimpl" did).

then have service instance injected automatically in controller adding following inside configureapplicationcontroller class:

@autowired appconfigureservice appconfigureservice; 

now can call this: appconfigureservice.getlistofallappconfigure();

note injection happen, need make sure have set componentscan property in configuration file scan packages contain classes injected. in case, package contains appconfigureserviceimpl.

<context:component-scan base-package="com.my.servicepackage" /> 

note should same dao instead of creating new application context , getting there. i.e. add

@autowired appconfiguredao appconfiguredao; 

property inside appconfigureserviceimpl , use that.


Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -