How to use annotation and avoid xml configuration in spring framework -


i have designed packing structure.

  • controller
  • delegates (which helper class) - class business , return value controllers.
  • service
  • service implementation
  • dao
  • dao implementation.

i want implement autowired (annotation) concept , avoid xml configuration such service , dao configuration on spring-bean.xml.

this code not working if want avoid xml configuration.

i have done changes

  • bean id :logindelegate, userservice, userdao
  • added @service & @repository annotation corresponding service & dao implementation.

@controller("logincontroller") public class logincontroller {      @autowired     private logindelegate logindelegate;      public logindelegate getlogindelegate() {         return this.logindelegate;     }     public void setlogindelegate(logindelegate tlogindelegate) {         this.logindelegate = tlogindelegate;     }     @requestmapping(value="/login.do",method=requestmethod.get)     public modelandview displaylogin(httpservletrequest request, httpservletresponse response) {         log.info("<---displaylogin()--->");         modelandview model = new modelandview("login");         loginbean loginbean = new loginbean();         model.addobject("loginbean", loginbean);         return model;     } }     public class logindelegate {      @autowired      private iuserservice userservice;       public iuserservice getuserservice() {          return this.userservice;      }       public void setuserservice(iuserservice userservice) {          this.userservice = userservice;      }       public boolean isvaliduser(string username, string password) throws exception {          return userservice.isvaliduser(username, password);      } }  public interface iuserservice {     public boolean isvaliduser(userbean userobj);     public int addusers(userbean userobj); }  public class userserviceimpl implements iuserservice {     @autowired     private iuserdao userdao;      public iuserdao getuserdao()  {         return this.userdao;     }      public void setuserdao(iuserdao userdao) {         this.userdao = userdao;     }      public boolean isvaliduser(userbean userobj) {         return userdao.isexistuser(userobj);     }      @override     public int adduser(final userbean userobj) {         return userdao.saveuserdetails(userobj);         } }  public interface iuserdao {     public boolean isexistuser(userbean userobj);     public int saveuserdetails(userbean userobj); }  public class userdaoimpl implements iuserdao {     @autowired     userbean userobj;      @autowired     datasource datasource ;      public datasource getdatasource(){         return this.datasource;     }      public void setdatasource(datasource datasource){         this.datasource = datasource;     } 

use java-based configuration if want rid of xml-based configuration

@configuration @componentscan(basepackages = "com.acme") public class appconfig  {     ... } 
  1. the above normal java class when annotated @configuration, makes 'spring configuration class' (analogous xml-based configuration).
  2. @componentscan annotation scans classes annotated @component, @controller, @service, @repository classes package defined during start-up time them registered spring beans. can done in xml <context:component-scan base-package="com.acme" />

refer:http://docs.spring.io/spring-framework/docs/current/spring-framework-reference/html/beans.html#beans-java-instantiating-container-scan


Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -