java - Spring中字段的不满足依赖错误

标签 java spring spring-jdbc jdbctemplate

下面的代码工作正常,但如果我评论 OracleConfiguration 类中的 jdbcTemplateRandomName 方法,我会收到以下错误:-

我试图通过注释 jdbcTemplateRandomName 方法来理解为什么我收到错误 错误出现在 BaseDaoImpl 类中。

Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'baseDaoImpl': Unsatisfied dependency expressed through field 'jdbcTemplate'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.jdbc.core.JdbcTemplate' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.jdbc.core.JdbcTemplate' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

@Component
    public class BaseDaoImpl {

        @Autowired
        private JdbcTemplate jdbcTemplate;


            public JdbcTemplate getJdbcTemplate() {
                                return jdbcTemplate;
            }
@Configuration   
 public class OracleConfiguration {

        @Bean
        DataSource dataSource() throws SQLException {

            OracleDataSource dataSource = new OracleDataSource();
            //removed code for brevity setting username,password to datasource
            return dataSource;
        }

        @Bean
         public JdbcTemplate jdbcTemplateRandomName(DataSource dataSource) {
           JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
           jdbcTemplate.setResultsMapCaseInsensitive(true);
            return jdbcTemplate;
        }

    public class RolesDaoImpl  extends BaseDaoImpl implements RolesDao  {

    //removed lot of unnecessary  code for the question
    List<Roles> rolesList  = getJdbcTemplate().query(sql,
                    new BeanPropertyRowMapper<Roles>(Roles.class));

最佳答案

如果您注释 jdbcTemplateRandomName() 方法,您将从 Spring IoC 配置中删除 JdbcTemplate bean 的声明。因此 Spring 将无法找到合适的对象来填充 BaseDaoImpl 类的 jdbcTemplate 属性

关于java - Spring中字段的不满足依赖错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52485699/

相关文章:

java - 为什么Maven找不到org.json JPMS自动模块?

Java:比较 2 个数字的集合比连接更好的方法

java - sitemesh和spring MVC装饰器模式问题

java - Spring:java.io.NotSerializedException:MapSqlParameterSource

spring - 这些 Spring 调试消息是什么意思?

java - WAR 文件的 Azure 部署显示空白页面

Spring Boot 允许在请求参数中使用方括号嵌套对象

spring - 如何在 AuthenticationSuccessHandler 中访问 OAuth2AccessTokenResponse?

java - 使用 simplejdbccall 执行存储过程的替代方法

java - BottomSheetDialogFragment关闭监听器