springboot 使用druidDataSource,用配置文件自动配置,如果想把DataSource注入该怎么做 ?

工程中使用的式druidDataSource,没有写任何Java配置文件,直接使用properties文件创建的数据源。现在想使用shiro,jdbcreadlm使用时需要set数据源,我直接在配置文件上方使用autowired会报错。请问有什么解决方案么?。

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/random_recommend?characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=root@password
#druid数据源需要的东西
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.druid.max-active=20
spring.datasource.druid.test-on-borrow=true
    @Autowired
    DruidDataSource druidDataSource;
    @ExceptionHandler(AuthorizationException.class)
    @ResponseStatus(HttpStatus.FORBIDDEN)
    public String handleException(AuthorizationException e, Model model) {

        // you could return a 404 here instead (this is how github handles 403, so the user does NOT know there is a
        // resource at that location)
        LogUtil.debug(this.getClass(),"AuthorizationException was thrown", e);

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("status", HttpStatus.FORBIDDEN.value());
        map.put("message", "用户权限不足");
        model.addAttribute("errors", map);

        return "error403";
    }

    @Bean
    public Realm realm() {
        JdbcRealm jdbcRealm = new JdbcRealm();
        jdbcRealm.setDataSource(druidDataSource);
        jdbcRealm.setPermissionsLookupEnabled(true);
        String sqlQueryUserPassword = "select user_password from tb_sys_user where user_id = ?";
        jdbcRealm.setAuthenticationQuery(sqlQueryUserPassword);
        return jdbcRealm;
    }
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问