【问题标题】:Error creating dataSource bean创建数据源 bean 时出错
【发布时间】:2018-05-18 04:24:39
【问题描述】:

dataSource 在此类中定义,我在 springSecurityConfig.java 类中使用相同的 bean,但它给了我错误: No qualifying bean of type 'javax.sql.DataSource' available

ShoppingServletConfig.java

@Configuration
@EnableWebMvc
@EnableTransactionManagement
@ComponentScan(basePackages = "com.project.shopping")
public class ShoppingServletConfig {
 @Primary
 @Bean(name = "dataSource")
    public DataSource dataSource() {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/shopping");
        dataSource.setUsername("root");
        dataSource.setPassword("root");
        return dataSource;
    }

   }

SpringSecurityConfig.java

@Configuration
@EnableWebSecurity

public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
@Qualifier("dataSource")
DataSource dataSource;
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    auth
    .jdbcAuthentication()
    .dataSource(dataSource)
    .usersByUsernameQuery(
               "select username,password, enabled from user where user_name=?")
              .authoritiesByUsernameQuery(
               "select username, role from user_roles where user_name=?");
}

控制台中的错误是这样的:

 org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'springSecurityConfig':
 Unsatisfied dependency expressed through field 'dataSource'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.sql.DataSource' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=dataSource)}

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.sql.DataSource' available: expected at least 1 bean which qualifies as autowire candidate.
 Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=dataSource)}

【问题讨论】:

  • 检查SpringSecurityConfig是在com.project.shopping包下还是它的子包下。
  • @shazin 它在 com.project.shopping.configuration 包下'ShoppingServletConfig'也在同一个包上

标签: java spring spring-mvc spring-security


【解决方案1】:

我有时会这样做,强制 Spring 对显式 bean 进行依赖管理。 配置接口作为匿名类返回:

@Configuration
@EnableWebSecurity
public class SpringSecurityConfig {

@Bean
public WebSecurityConfigurerAdapter securityAdapter (DataSource dataSource) {
    return new WebSecurityConfigurerAdapter () {
        @Override
        public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
            auth.jdbcAuthentication()
                .dataSource(dataSource)
                .usersByUsernameQuery("select username,password, enabled from user where user_name=?")
                .authoritiesByUsernameQuery("select username, role from user_roles where user_name=?");
      }
   }
}

【讨论】:

  • 它给出了同样的问题
  • 将 '@ComponentScan(basePackages = "com.project.shopping")' 添加到 SpringSecurityConfig.java 解决了这个问题。我们是否需要添加 @ComponentScan(basePackage="#") 以在使用 @Configuration 注释的类之间进行 bean 通信?
  • 是的,如果您不使用自动自动扫描的Spring Boot,那么您需要使用@Import({SpringSecurityConfig.class}) 或基类配置扫描显式加载上述配置(就像您所做的那样)与@ComponentScan
猜你喜欢
  • 1970-01-01
  • 2012-12-16
  • 1970-01-01
  • 2016-06-24
  • 2017-08-20
  • 2013-10-06
  • 2015-06-29
  • 2018-04-07
  • 2017-09-24
相关资源
最近更新 更多