首页 > 其他分享 >Property 'dataSource' is required

Property 'dataSource' is required

时间:2023-05-11 19:58:29浏览次数:42  
标签:required dataSource PersistentTokenRepository private DataSource JdbcTokenReposi

写了一个配置类,启动项目的时候报错:Property 'dataSource' is required
原代码如下:

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private PersistentTokenRepository persistentTokenRepository;

    @Autowired
    private DataSource dataSource;

    @Bean
    PersistentTokenRepository getPersistentTokenRepository(){
        JdbcTokenRepositoryImpl jdbcTokenRepository = new JdbcTokenRepositoryImpl();
        DataSource dataSource = this.dataSource;
        jdbcTokenRepository.setDataSource(dataSource);
        return jdbcTokenRepository;
    }
}

解决办法:把DataSource 放到最上面即可,如下所示。spring在加载bean的时候,从上而下加载,按照上面的代码,先加载PersistentTokenRepository,此时还没加载DataSource,因此DataSource=null,jdbcTokenRepository.setDataSource(dataSource)这个方法执行的时候就会报错。调整了一下位置,问题解决

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
  
  
    @Autowired
    private DataSource dataSource;

    @Autowired
    private PersistentTokenRepository persistentTokenRepository;


    @Bean
    PersistentTokenRepository getPersistentTokenRepository(){
        JdbcTokenRepositoryImpl jdbcTokenRepository = new JdbcTokenRepositoryImpl();
        DataSource dataSource = this.dataSource;
        jdbcTokenRepository.setDataSource(dataSource);
        return jdbcTokenRepository;
    }
}

标签:required,dataSource,PersistentTokenRepository,private,DataSource,JdbcTokenReposi
From: https://www.cnblogs.com/public-communion/p/17392039.html

相关文章