java - 创建数据源 bean 时出错

标签 java spring spring-mvc spring-security

dataSource 是在此类中定义的,我在 springSecurityConfig.java 类中使用相同的 bean,但它给了我错误: 没有可用的“javax.sql.DataSource”类型的合格 bean

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)}

最佳答案

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

@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=?");
      }
   }
}

关于java - 创建数据源 bean 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50403770/

相关文章:

java - 如何使用套接字发送位图?

java - 如何在 JdbcBatchItemWriter 中使用自动生成的 ID?

java - Spring转换服务的真实用例场景是什么

java - AndroidJavaProxy 不是一个接口(interface)

java - 在java中从xml设置数字

java - 如何将 map 值放入不同类型的列表中?

java - 使用带有 Struts2 的 Spring Security 的表单的动态 URL 访问权限?

java - NamedParameterJdbcTemplate - 从中​​选择 *

java - yml配置中的spring boot spring EL

java - 如何在启动时加载应用程序范围的设置(对于 Spring3 webapp)?