当前位置: 首页 > 知识库问答 >
问题:

Spring批处理@主注释重写错误bean

祝英博
2023-03-14

我按照本教程在Java中配置Spring批处理作业。它通过使用一个接口为多个数据源做准备,然后由每个数据源实现该接口。

这是我目前所掌握的:

public interface InfrastructureConfiguration {
    @Bean
    DataSource dataSource();
}
@Configuration
@Primary
public class MySQLConfiguration implements InfrastructureConfiguration {

    @Bean
    public DataSource dataSource() {
        final DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/employees?useSSL=false");
        dataSource.setUsername("testing");
        dataSource.setPassword("testing");
        return dataSource;
    }
}

PostgreSQLConfig.java

@Configuration
public class PostgreSQLConfiguration implements InfrastructureConfiguration {

    @Bean
    public DataSource dataSource() {
        final DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("org.postgresql.Driver");
        dataSource.setUrl("jdbc:postgresql://localhost:5432/postgres");
        dataSource.setUsername("postgres");
        dataSource.setPassword("testing");
        return dataSource;
    }
}

jobconfig.java

@Configuration
public class JobConfig {

    @Autowired
    private InfrastructureConfig infrastructureConfig

    ....
}

通过对我的MySQLConfig使用@primary注释,我希望使用MySQLConfig bean。相反,我得到的是:

2017-03-09 12:46:21.422  INFO 1496 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'dataSource' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=mySQLConfiguration; factoryMethodName=dataSource; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [config/MySQLConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=postgreSQLConfiguration; factoryMethodName=dataSource; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [config/PostgreSQLConfiguration.class]]

共有1个答案

卞博简
2023-03-14

@primary放在方法上(@bean旁边),而不是放在类级别上。

 类似资料:
  • 主要内容:使用Rem语句注释,注释使用::声明为创建的脚本添加注释或文档总是一个好习惯。 这是一个维护脚本用来理解脚本实际所做的事情所必需的注释。 例如,考虑下面这段没有注释形式的代码。 如果一个没有任何注释的脚本,普通人试图理解脚本,那么需要很多时间来理解脚本做些什么工作。 使用Rem语句注释 有两种方法可以在批处理脚本中创建注释; 一个是通过命令。 语句后的任何文本都将被视为注释,不会被执行。 以下是此声明的一般语法。 语法 其中是需要添

  • 我开始学习spring batch,遇到一个问题,当我想使用在数据库中持久化作业的状态时。编译器显示: “原因:org.springframework.beans.factory.beanCreationException:创建类路径资源[springconfig.xml]中定义的名为'job repository'的bean时出错:调用init方法失败;嵌套异常为java.lang.noClas

  • 在添加com.google.dagger:dagger-android-support依赖项后,我得到了这个gradle错误。 这是我的构建的一部分

  • 我用Spring Boot创建了一个批处理。以下是批处理的主要配置类: 当处理项目时发生错误,记录器会写入一条消息,然后批处理下一个元素。这正是我想要的。 如果写入操作出错,请重试一次!因此,对于每个写入错误,我有两个错误日志。 如何将批处理配置为无论错误发生在步骤的哪个部分,都不会在出错时重试? 本文解释如下: [当我们在编写过程中出现跳转时],框架必须找出导致失败的项目。对于缓存的读取项列表中