**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 is returning null** ***Why?*** but in my web application everything works good.

I Try lot of things like:

* Create a new sessionFactory but doesn't work.
* Used: classes ={AppConfig.class,SpringMVCConfig.class}.
* Used: @ContextConfiguration(initializers = {SpringMVCInitializer.class}, loader=AnnotationConfigContextLoader.class) throw Type mismatch: cannot convert from Class to Class>
* Used: @SpringApplicationConfiguration(classes = {SpringMVCInitializer.class})