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

一个组件需要一个名为“dataSource”的bean,但找不到

段干高歌
2023-03-14

我正在使用Spring Boot开发一个简单的Spring Batch jar。我已经使用配置类创建了dataSource bean,并用@Component进行了注释。但是当我使用命令行Runner运行应用程序时,它在读取ABPBatchInfrastructure.xml时抛出bean not found异常。

我在谷歌上对这个错误做了一些研究,找到了一个解决方案,我在ABPBatchInfrastructure.xml中添加了下面一行

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

添加此行已修复问题,但有其他副作用

> < li>

Spring加载了两次,所有的Spring核心beans都实例化了两次。我通过检查日志找到了这个。以下日志供参考,其中显示相同的线两次。

即使在application-dev.properties.中添加spring.jpa.properties.hibernate.format_sql=true,Spring JPASQL语句也不会显示在控制台中

有没有一种方法可以通过删除xml中的组件扫描来实例化spring一次,并解决datasource bean not found的问题?请指引我。下面的日志供你参考,它清楚地显示了spring正在加载两次。

    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();    
    }
}

共有1个答案

景品
2023-03-14

如前所述,我已经在应用启动期间导入了 xml,并在 run 方法中删除了应用程序上下文初始化。此外,我还删除了 XML 中的组件扫描。这已修复了“找不到数据源”问题和“显示 SQL”问题。现在应用程序按预期工作。以下是我的新入口点类。感谢您的帮助:)

    @SpringBootApplication
@ComponentScan(basePackages = "com.abp.printbatch")
@ImportResource( { "ABPBatchInfrastructure.xml", "AgencyBillPayAppConfig.xml" } ) 
public class FileUploadApplication extends PrintBatchConstants implements CommandLineRunner {

    @Autowired
    private NotifyYaml notify;

    @Autowired
    private ApplicationContext ctx;

    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);
      }

     }
 类似资料: