Java Spring Security找不到数据源

Java Spring Security找不到数据源,java,mysql,authentication,spring-boot,spring-security,Java,Mysql,Authentication,Spring Boot,Spring Security,我不熟悉Spring引导和Spring安全性,我正在尝试设置Spring安全性以使用MySQL数据库中的登录凭据 SecurityConfig类: @Service @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { private DataSource dataSource; public void

我不熟悉Spring引导和Spring安全性,我正在尝试设置Spring安全性以使用MySQL数据库中的登录凭据

SecurityConfig类:

   @Service
   @Configuration
   @EnableWebSecurity
    public class SecurityConfig extends WebSecurityConfigurerAdapter {

    private DataSource dataSource;

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }


@Override
protected void configure(HttpSecurity http) throws Exception {
    http
            .authorizeRequests().anyRequest().authenticated();
    http
            .formLogin().failureUrl("/login?error")
            .defaultSuccessUrl("/")
            .loginPage("/login")
            .permitAll()
            .and()
            .logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout")).logoutSuccessUrl("/login")
            .permitAll();
}

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {

    auth
            .jdbcAuthentication().dataSource(dataSource)
            .usersByUsernameQuery("SELECT username, password, enabled FROM users WHERE username = ?")
            .authoritiesByUsernameQuery("SELECT username as username, enabled as authority FROM users WHERE username = ?");
    }
}
spring.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="userDaoImpl" class="com.watchdog.dao.UserDaoImpl">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <!--<property name="url" value="jdbc:mysql://localhost:3306/bsbuckne" />-->
        <property name="url" value="jdbc:mysql://localhost:3306/test" />
        <property name="username" value="root" />
        <property name="password" value="password" />
    </bean>
</beans>
如果我将身份验证类型更改为内存中,并手动设置用户名和密码,则身份验证有效


我之所以如此困惑,是因为我在处理所有数据库CRUD操作的不同DAO类中使用了相同的setDataSource方法,并且它按预期工作。什么使我的SecurityConfig类无法找到我的数据源?

请尝试注释:

@Autowired
private DataSource dataSource;


我肯定试过第一种。我认为,由于我使用的是XML方法,而不是带注释的数据源bean,所以简单地自动连接数据源对我来说并不可行。它似乎在工作,但我的IDE中出现了一条红色的曲线,表示“找不到“datasource”类型的bean”。我的堆栈跟踪不再出现,所以它可能只是IntelliJ。我是IntelliJ IDEA中另一个正在努力解决这个问题的人,我认为警告是一个真正的编译错误。无论如何,当我在IDE中出现错误的情况下启动应用程序时,它运行良好。
@Autowired
private DataSource dataSource;
@Autowired
public void setDataSource(DataSource dataSource) {
    this.dataSource = dataSource;
}