组件需要找不到名为“ dataSource”的bean

我正在使用Spring Boot开发一个简单的Spring Batch jar。我已经使用Configuration类创建了dataSource bean,并使用@Component进行了注释。但是,当我使用CommandLine Runner运行应用程序时,在读取ABPBatchInfrastructure.xml时会抛出未找到的bean异常。

我对谷歌中的此错误进行了一些研究,找到了解决方案,我在ABPBatchInfrastructure.xml中添加了以下内容

   <context:component-scan base-package="com.abp.printbatch"></context:component-scan>

添加此行已解决此问题,但有其他副作用

  1. Spring加载两次,所有spring核心bean实例化两次。
  2. 我通过检查日志发现了这一点。以下日志作为参考,两次显示相同的行。
  3. 即使在application-dev.properties中添加spring.jpa.properties.hibernate.format_sql = true之后,控制台中也不会显示Spring JPA SQL语句。

有没有一种方法可以通过删除xml中的组件扫描来仅实例化spring一次,并且还可以解决datasource bean找不到问题。请指导我。下面的日志供您参考,清楚地表明弹簧加载了两次。

    2020-05-23 16:04:06.976  INFO 90732 --- [           main] c.a.p.FileUploadApplication : Starting FileUploadApplication on MW7CH1-FZXX with PID 90732 (C:\gitforABP\SpringBatch\target\classes started by cac6584 in C:\gitforABP\SpringBatch)
2020-05-23 16:04:06.979  INFO 90732 --- [           main] c.a.p.FileUploadApplication : The following profiles are active: dev
2020-05-23 16:04:07.672  INFO 90732 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2020-05-23 16:04:07.752  INFO 90732 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 69ms. Found 1 JPA repository interfaces.

2020-05-23 16:04:08.161  INFO 90732 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2020-05-23 16:04:08.165  WARN 90732 --- [           main] com.zaxxer.hikari.util.DriverDataSource  : Registered driver with driverClassName=oracle.jdbc.driver.OracleDriver was not found, trying direct instantiation.
2020-05-23 16:04:08.819  INFO 90732 --- [           main] com.zaxxer.hikari.pool.PoolBase          : HikariPool-1 - Driver does not support get/set network timeout for connections. (Receiver class oracle.jdbc.driver.T4CConnection does not define or inherit an implementation of the resolved method abstract getNetworkTimeout()I of interface java.sql.Connection.)
2020-05-23 16:04:08.870  INFO 90732 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2020-05-23 16:04:08.923  INFO 90732 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2020-05-23 16:04:09.006  INFO 90732 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.4.12.Final
2020-05-23 16:04:09.152  INFO 90732 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
2020-05-23 16:04:09.312  INFO 90732 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.Oracle10gDialect
2020-05-23 16:04:10.351  INFO 90732 --- [           main] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2020-05-23 16:04:10.364  INFO 90732 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2020-05-23 16:04:10.771  INFO 90732 --- [           main] c.a.p.FileUploadApplication : Started FileUploadApplication in 4.167 seconds (JVM running for 5.839)
2020-05-23 16:04:10.772  INFO 90732 --- [           main] c.a.p.FileUploadApplication : Local
2020-05-23 16:04:11.186  INFO 90732 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2020-05-23 16:04:11.206  INFO 90732 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 19ms. Found 1 JPA repository interfaces.

2020-05-23 16:04:11.298  INFO 90732 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-2 - Starting...
2020-05-23 16:04:11.298  WARN 90732 --- [           main] com.zaxxer.hikari.util.DriverDataSource  : Registered driver with driverClassName=oracle.jdbc.driver.OracleDriver was not found, trying direct instantiation.
2020-05-23 16:04:11.663  INFO 90732 --- [           main] com.zaxxer.hikari.pool.PoolBase          : HikariPool-2 - Driver does not support get/set network timeout for connections. (Receiver class oracle.jdbc.driver.T4CConnection does not define or inherit an implementation of the resolved method abstract getNetworkTimeout()I of interface java.sql.Connection.)
2020-05-23 16:04:11.691  INFO 90732 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-2 - Start completed.
2020-05-23 16:04:11.706  INFO 90732 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2020-05-23 16:04:11.714  INFO 90732 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.Oracle10gDialect
2020-05-23 16:04:12.150  INFO 90732 --- [           main] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2020-05-23 16:04:12.151  INFO 90732 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2020-05-23 16:04:12.170  INFO 90732 --- [           main] o.s.b.c.r.s.JobRepositoryFactoryBean     : No database type set, using meta data indicating: ORACLE
2020-05-23 16:04:12.264  INFO 90732 --- [           main] o.s.b.c.l.support.SimpleJobLauncher      : No TaskExecutor has been set, defaulting to synchronous executor.
2020-05-23 16:04:12.465  WARN 90732 --- [           main] o.s.b.a.batch.JpaBatchConfigurer         : JPA does not support custom isolation levels, so locks may not be taken when launching Jobs
2020-05-23 16:04:12.467  INFO 90732 --- [           main] o.s.b.c.r.s.JobRepositoryFactoryBean     : No database type set, using meta data indicating: ORACLE
2020-05-23 16:04:12.467  INFO 90732 --- [           main] o.s.b.c.l.support.SimpleJobLauncher      : No TaskExecutor has been set, defaulting to synchronous executor.

入口点

@SpringBootApplication
@ComponentScan(basePackages = "com.abp.printbatch")
public class FileUploadApplication extends PrintBatchConstants implements CommandLineRunner {

    @Autowired
    private NotifyYaml notify;

    final static Logger logger = LoggerFactory.getLogger(FileUploadApplication.class);

    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(FileUploadApplication.class);

        application.setBannerMode(Banner.Mode.OFF);
        application.run(args);

    }

      @Override 
      public void run(String... args) throws Exception {

      logger.info(notify.getEnvironment());


      ClassPathXmlApplicationContext  ctx = new ClassPathXmlApplicationContext(
          "ABPBatchInfrastructure.xml", "AgencyBillPayAppConfig.xml" );



          JobLauncher jobLauncher = ctx.getBean(JobLauncher.class); 
          Job job=ctx.getBean(Job.class);

          jobLauncher.run(job, new JobParametersBuilder()
                  .addString(documentClass,"InvoiceStatementDocumentation")
                  .addString(type, "2040-09-13")
                  .addString(emailID, notify.getSupportEmailId())
                  .addString(environment, notify.getEnvironment())
                  .toJobParameters());

        ctx.close();

          System.exit(0);
      }

     }

F

package com.abp.printbatch.config;

@Configuration
@Component
public class DBConfig {


    @Bean
    @Primary
     public DataSource dataSource() {
        System.out.println("");
        return DataSourceBuilder.create().driverClassName("oracle.jdbc.driver.OracleDriver").url("removed")
               .username("removed").password("removed").build();    
    }
}
评论