title: Could not autowire field: javax.sql.DataSource
body: <p>I created Spring security project connect mysql to authentication </p>
  
  <p>I have 2 class:</p>
  
  <p><strong>SpringWebConfig.java:</strong></p>
  
  <pre><code>@EnableWebMvc
      @Configuration
      @ComponentScan({ "com.tructran.web" })
      @Import({ SecurityConfig.class })
      public class SpringWebConfig extends WebMvcConfigurerAdapter {
  
          @Override
          public void addResourceHandlers(ResourceHandlerRegistry registry) {
              registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
          }
  
          @Bean
          public InternalResourceViewResolver viewResolver() {
              InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
              viewResolver.setViewClass(JstlView.class);
              viewResolver.setPrefix("/WEB-INF/views/jsp/");
              viewResolver.setSuffix(".jsp");
              return viewResolver;
          }
  
          @Bean(name = "dataSource")
          public DriverManagerDataSource dataSource() {
              DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
              driverManagerDataSource.setDriverClassName("com.mysql.jdbc.Driver");
              driverManagerDataSource.setUrl("jdbc:mysql://localhost:3306/edc_new");
              driverManagerDataSource.setUsername("truc.tran");
              driverManagerDataSource.setPassword("652606");
                  return driverManagerDataSource;
              }
  
          }
  </code></pre>
  
  <p><strong>SecurityConfig.java:</strong> </p>
  
  <pre><code>@Configuration
  @EnableWebSecurity
  public class SecurityConfig extends WebSecurityConfigurerAdapter {
  
      @Autowired
      DataSource dataSource;
  
      @Autowired
      public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {
  
          auth.jdbcAuthentication().dataSource(dataSource)
                  .usersByUsernameQuery("select username,password, enabled from users where username=?")
                  .authoritiesByUsernameQuery("select username, role from user_roles where username=?");
      }
  
      @Override
      protected void configure(HttpSecurity http) throws Exception {
          http.authorizeRequests().antMatchers("/admin/**").access("hasRole(ROLE_ADMIN)").and().formLogin();
      }
  </code></pre>
  
  <p><strong>When I run project It has error:</strong></p>
  
  <pre><code>   Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: javax.sql.DataSource com.tructran.config.SecurityConfig.dataSource; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
          at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:517)
          at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
          at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:286)
          ... 22 more
  </code></pre>
  
  <p>Please help me!!!</p>
  
  <p>Thanks so much :)</p>