java - (Spring MVC + Hibernate 4 + Test 4) autowired DAO return NULL -


maven dependencies

<!-- spring mvc --> <spring-version>4.0.3.release</spring-version> <spring-test-version>4.2.1.release</spring-test-version> <!-- tests --> <junit-version>4.11</junit-version> <!-- data base --> <hibernate.version>4.1.5.final</hibernate.version> <hibernate-validator>4.2.0.final</hibernate-validator> <mysql.connector.version>5.1.32</mysql.connector.version> 

accountdao.java

@repository @transactional public class accountdao {      private final sessionfactory sessionfactory;      @inject     public accountdao(sessionfactory sessionfactory) {         this.sessionfactory = sessionfactory;      }     [...] } 

test

@runwith(springjunit4classrunner.class) @contextconfiguration(classes = {springmvcinitializer.class}, loader=annotationconfigcontextloader.class) public class loadingtests extends testmachine {      @autowired     private accountdao accountdao; 

appconfig.java

 @configuration  @propertysource("classpath:application.properties")  @componentscan(basepackages = "company")  @enabletransactionmanagement  public class appconfig {         @bean          public accountdao accountdao() {              return new accountdao();          }          @bean         public propertysourcesplaceholderconfigurer propertyplaceholderconfigurer() {             return new propertysourcesplaceholderconfigurer();         }          @bean         public jdbctemplate jdbctemplate() {             return new jdbctemplate(datasource());         }          @bean         public datasource datasource() {             drivermanagerdatasource datasource = new drivermanagerdatasource();              /*local*/             datasource.setdriverclassname("com.mysql.jdbc.driver");             datasource.seturl("jdbc:mysql://localhost:3306/compamny");             datasource.setusername("root");             datasource.setpassword("");              return datasource;         }          @bean(name = "sessionfactory")         public localsessionfactorybean sessionfactory() {             localsessionfactorybean sessionfactorybean = new localsessionfactorybean();             sessionfactorybean.setdatasource(datasource());             sessionfactorybean.setpackagestoscan("insynctive.model");             sessionfactorybean.sethibernateproperties(hibproperties());             return sessionfactorybean;         }          @bean         public hibernatetransactionmanager transactionmanager() {             hibernatetransactionmanager transactionmanager = new hibernatetransactionmanager();             transactionmanager.setsessionfactory(sessionfactory().getobject());             return transactionmanager;         }          private properties hibproperties() {             properties properties = new properties();             properties.put(environment.hbm2ddl_auto, "create");             properties.put(environment.dialect, "org.hibernate.dialect.mysqldialect");             properties.put(environment.show_sql, true);             return properties;         }      } 

springmvcconfiguration.java

@configuration @enablewebmvc @componentscan(basepackages="company.controller") public class springmvcconfiguration extends webmvcconfigureradapter {      public void addresourcehandlers(resourcehandlerregistry registry) {         registry.addresourcehandler("/resources/**").addresourcelocations("/resources/");     }      @bean     public viewresolver viewresolver() {         internalresourceviewresolver viewresolverjsp = new internalresourceviewresolver();         viewresolverjsp.setorder(1);         viewresolverjsp.setviewclass(jstlview.class);         viewresolverjsp.setprefix("views/jsp/");         viewresolverjsp.setsuffix(".jsp");          return viewresolverjsp;     } } 

springmvcinitializer.java

public class springmvcinitializer extends abstractannotationconfigdispatcherservletinitializer {      @override     protected class<?>[] getrootconfigclasses() {         return new class<?>[] {appconfig.class, springmvcconfiguration.class};     }      @override     protected class<?>[] getservletconfigclasses() {         return null;     }      @override     protected string[] getservletmappings() {         return new string[] { "/" };     } } 

my autowired of accountdao returning null why? in web application works good.

i try lot of things like:

  • create new sessionfactory doesn't work.
  • used: classes ={appconfig.class,springmvcconfig.class}.
  • used: @contextconfiguration(initializers = {springmvcinitializer.class}, loader=annotationconfigcontextloader.class) throw type mismatch: cannot convert class class>
  • used: @springapplicationconfiguration(classes = {springmvcinitializer.class})

i think need set initializers instead of classes on loadingtests class.

@contextconfiguration(initializers = {springmvcinitializer.class}, loader=annotationconfigcontextloader.class) 

also please consider using constructor injection on dao.


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 -