无法自动装配字段:javax.sql.DataSource

时间:2017-05-02 10:58:03

标签: java spring

我创建了Spring安全项目,将mysql连接到身份验证

我有2个课程:

SpringWebConfig.java:

@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;
            }

        }

SecurityConfig.java:

@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();
    }

当我运行项目时出现错误:

   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

请帮帮我!!!

非常感谢:)

4 个答案:

答案 0 :(得分:0)

而不是:

@Bean(name = "dataSource")
public DriverManagerDataSource dataSource() {

}

将结果类型更改为:

@Bean(name = "dataSource")
public javax.sql.DataSource dataSource() {

}

答案 1 :(得分:0)

尝试删除@Import({ SecurityConfig.class })并确保SecurityConfig

中列出了@ComponentScan的包

答案 2 :(得分:0)

问题在于你的bean创建。这个DriverManagerDataSource不是javax.sql.DataSource,可以看到here。更改此实现:

@Bean
public DataSource dataSource() {
    DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
    dataSourceBuilder.url("jdbc:mysql://localhost:3306/edc_new");
    dataSourceBuilder.username("truc.tran");
    dataSourceBuilder.password("652606");
    dataSourceBuilder.driverClassName("com.mysql.jdbc.Driver");
    return dataSourceBuilder.build();   
}

答案 3 :(得分:0)

我遇到了类似的错误并通过添加以下依赖项来解决

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
    <version>2.5.0</version>
</dependency>