【问题标题】:Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name线程“主”org.springframework.beans.factory.UnsatisfiedDependencyException 中的异常:使用名称创建 bean 时出错
【发布时间】:2018-07-03 16:26:16
【问题描述】:

我正在使用 Spring Batch with Boot 并使用 XML 配置来配置批处理作业。但是批处理作业是使用 Tivoli 调度的。我希望通过使用批处理启动器在本地运行。

我们怎样才能满足依赖?

Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'panAccountRangeDao' defined in file [C:\Project\Core\src\main\resources\META-INF\spring\XXX-batch-job.xml]: Unsatisfied dependency expressed through constructor parameter 0: Ambiguous argument values for parameter of type [javax.sql.DataSource] - did you specify the correct bean references as arguments?
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:736)
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:189)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1143)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1046)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:776)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)
    at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:140)
    at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:94)
    at com.mastercard.ess.eds.CustomerFileGenerationApp.main(CustomerFileGenerationApp.java:29)

当我配置 JobLauncher 时

<bean id="transactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />

<bean id="jobRepository" class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean" >
    <property name="dataSource" ref="ABCDataSource" />
    <property name="transactionManager" ref="transactionManager" />
    <property name="databaseType" value="Oracle" />
</bean>

<bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
    <property name="jobRepository" ref="jobRepository" />
</bean>

DatabaseConfig.java

@Configuration
@Component
@PropertySource("classpath:database.properties")
public class DatabaseConfig {
    private static Logger logger = Logger.getLogger(DatabaseConfig.class);

    @Value("${jdbc.url}")
    private String edsurl;

    @Value("${jdbc.driverClassName}")
    private String className;

    @Bean(name = "ABCDataSource")
    public DataSource batchABCDataSource() throws IOException {

        logger.info("Enter into BatchABCDataSource method");

        OracleDataSource ds = null;
        MCUserInfoFactory userInfoFactory = new MCUserInfoFactory();
        try {
            MCUserInfo userInfo = userInfoFactory.getMCUserInfo(XXLabel);
            if (userInfo != null) {
                ds = getDataSource(userInfo, XXXurl);
            }

        } catch (Exception e) {
            logger.error("Exception occured while initializing the EDS datatsource" + e);
            throw new IOException("Exception occured while initializing the EDS datatsource", e);
        }

        logger.info("Exit into BatchABCDataSource method");

        return ds;
    }


    private OracleDataSource getDataSource(MCUserInfo userInfo , String url) throws SQLException {
        OracleDataSource ds = new OracleDataSource();
        ds.setURL(url);
        ds.setUser(userInfo.getUserid());
        ds.setPassword(userInfo.getPassword());
        Properties properties = new Properties();
        properties.setProperty(MIN_LIMIT, minPoolSize.toString());
        properties.setProperty(MAX_LIMIT, maxPoolSize.toString());
        ds.setConnectionCacheProperties(properties);
        return ds;
    }
}

XML 配置:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:batch="http://www.springframework.org/schema/batch" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:task="http://www.springframework.org/schema/task" xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/task  http://www.springframework.org/schema/task/spring-task.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

    <batch:job id="ABCProcessJob">
        <batch:step id="abcDataInCache">
            <batch:tasklet ref = "cacheLoader" />
            <batch:next on="COMPLETED" to="ABCStep"/>
            <batch:fail on="FAILED" exit-code="XXXXXXX" /> 
        </batch:step>
        <batch:step id="ABCStep">
            <partition partitioner="abcPartitioner" handler="ABCProcessHandler">
            </partition>
        </batch:step>
    </batch:job>

    <bean id="ABCProcessHandler" class="org.springframework.batch.core.partition.support.TaskExecutorPartitionHandler" scope="step">
        <property name="taskExecutor" ref="ABCTaskExecutor" />
        <property name="step" ref="processBatch" />
        <property name="gridSize" value="20" />
    </bean>

    <batch:step id="processBatch">  
        <batch:tasklet>
            <batch:chunk reader="ABCRecordItemReader" writer="abcRecordDBWriter"
                processor="panProcessor" commit-interval="100" skip-limit="60000" processor-transactional="false">
                <batch:skippable-exception-classes>
                    <batch:include class="org.springframework.batch.item.validator.ValidationException" />
                    <batch:include class="com.XXXX.abcException" />
                </batch:skippable-exception-classes>
                <batch:listeners>
                    <batch:listener>
                        <bean class="com.XXXXXXXXXXX.listener.RawRecordSkipListener" scope="step">
                            <property name="jobInstanceName" value="#{stepExecution.jobExecution.jobInstance.jobName}"/>
                            <property name="jobInstanceId" value="#{stepExecution.jobExecution.jobId}"/>
                        </bean>
                    </batch:listener>
                </batch:listeners>
            </batch:chunk>
        </batch:tasklet>
    </batch:step>
</beans>

【问题讨论】:

  • 可能是java-config和xml-config混合产生的错误。最好的建议是只使用一种类型来配置您的应用程序
  • 杰夫,你是怎么解决这个问题的?我也在撞墙。首先是构造函数有一个原始布尔值作为第 0 个参数。我对此进行了评论,现在它正在提醒现在是第 0 个参数的字符串可变参数。这是一个非常模糊和无用的例外

标签: spring spring-boot spring-batch


【解决方案1】:

如堆栈跟踪中所述,我认为您需要仔细检查 panAccountRangeDao 的构造函数。 他们有第一个参数是javax.sql.DataSource 类型吗?

【讨论】:

  • public XXXXRangeDAO(@Autowired @Qualifier("rABCDataSource")DataSource 数据源) { jdbcTemplate = new JdbcTemplate(datasource); }
  • 将自动装配注释放在构造函数的顶部,它应该可以工作。类似@Autowired public XXXXRangeDAO(@Qualifier("rABCDataSource")DataSource datasource) { jdbcTemplate = new JdbcTemplate(datasource); }
  • 我已经用过@Autowired。还有其他建议吗?它的阻塞
猜你喜欢
  • 2017-03-28
  • 2020-02-23
  • 2016-12-26
  • 2017-08-20
  • 2019-01-22
  • 2021-11-17
  • 2021-08-25
  • 2021-08-13
相关资源
最近更新 更多