Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid bean definition with name 'employeesJob' defined in class path resource: factory-bean reference points back to the same bean definition

I went through this link : Spring Bean Inheritance Using Annotations Bean reference back issue and other links as well, but it did not solved my issue yet.

I am working on Spring Boot Batch example. In this example I am getting the below error -

org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'employeesJob' defined in class path resource [com/prateek/job/EmployeesJob.class]: factory-bean reference points back to the same bean definition
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryMethod(AbstractAutowireCapableBeanFactory.java:703) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineTargetType(AbstractAutowireCapableBeanFactory.java:667) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:635) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1489) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1012) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.requiresEagerInitForType(DefaultListableBeanFactory.java:498) [spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(DefaultListableBeanFactory.java:418) [spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:390) [spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
        at org.springframework.context.support.PostProcessorRegistrationDelegate.registerBeanPostProcessors(PostProcessorRegistrationDelegate.java:189) [spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:710) [spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:535) [spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:395) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1243) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
        at com.prateek.SpringBatchClassicDbApplication.main(SpringBatchClassicDbApplication.java:10) [classes/:na]

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dataSourceInitializerPostProcessor': Unsatisfied dependency expressed through field 'beanFactory'; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'employeesJob' defined in class path resource [com/prateek/job/EmployeesJob.class]: factory-bean reference points back to the same bean definition
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:587) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:91) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:373) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1350) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:580) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:503) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:204) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.context.support.PostProcessorRegistrationDelegate.registerBeanPostProcessors(PostProcessorRegistrationDelegate.java:226) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:710) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:535) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:395) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1243) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
    at com.prateek.SpringBatchClassicDbApplication.main(SpringBatchClassicDbApplication.java:10) [classes/:na]

EmployeesJob.java

@Configuration
@EnableBatchProcessing
public class EmployeesJob {
    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Autowired
    private DataSource dataSource;

    @Bean 
    public EmployeesProcessor employeesProcessor() {
        return new EmployeesProcessor();
    }

    @Bean
    public JdbcCursorItemReader<Employees> employeeReader1(){
        JdbcCursorItemReader<Employees> itemReader = new JdbcCursorItemReader<>();
        itemReader.setDataSource(dataSource);
        itemReader.setSql("SELECT employeeNumber, lastName, firstName, extension, email, officeCode, reportsTo, jobTitle FROM employees");
        itemReader.setRowMapper(new EmployeeMapper());
        itemReader.setFetchSize(200);
        return itemReader;
    }


    @Bean
    public FlatFileItemWriter<Employees> employeeWriter(){
        FlatFileItemWriter<Employees> fileItemWriter = new FlatFileItemWriter<>();
        fileItemWriter.setResource(new FileSystemResource("csv/employees.csv"));

        BeanWrapperFieldExtractor<Employees> fieldExtractor = new BeanWrapperFieldExtractor<>();
        fieldExtractor.setNames(new String[] {"employeeNumber", "lastName", "firstName", "extension", "email", "officeCode", "reportsTo", "jobTitle"});

        DelimitedLineAggregator<Employees> lineAggregator = new DelimitedLineAggregator<>();
        lineAggregator.setDelimiter(",");
        lineAggregator.setFieldExtractor(fieldExtractor);

        fileItemWriter.setLineAggregator(lineAggregator);
        fileItemWriter.setShouldDeleteIfEmpty(true);

        return fileItemWriter;
    } 


    @Bean
    public Step step1() {
        return stepBuilderFactory.get("step1")
                .<Employees, Employees>chunk(10)
                .reader(employeeReader1())
                .processor(new EmployeesProcessor())
                .writer(employeeWriter())
                .build();
    }

    @Bean
    public Job employeesJob() {
        return jobBuilderFactory.get("employeesJob")
                .incrementer(new RunIdIncrementer())
                .flow(step1())
                .end()
                .build();
    }
}

application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/classicmodels
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root


# SPRING BATCH (BatchProperties)
# Execute all Spring Batch jobs in the context on startup.
#spring.batch.job.enabled=true

# LOGGING
logging.level.org.springframework=DEBUG
logging.level.org.springframework.boot.autoconfigure=ERROR 

EmployeeProcessor

public class EmployeesProcessor implements ItemProcessor<Employees, Employees>{

    @Override
    public Employees process(Employees employees) throws Exception {
        return employees;
    }
}
like image 583
Jeff Cook Avatar asked Jul 07 '18 18:07

Jeff Cook


People also ask

Can a class with the name a bean be overridden?

A bean with that name has already been defined in class path resource [com/google/cloud/spring/autoconfigure/core/GcpContextAutoConfiguration.class] and overriding is disabled.

Should I enable Bean overriding?

Bean overriding can introduce major configuration issues and should not be enabled on non-test environments. You shouldn't enable bean definition overriding. You have no control over which definition you're going to get.

What is Bean overriding in Spring Boot?

Bean Overriding. Spring beans are identified by their names within an ApplicationContext. Thus, bean overriding is a default behavior that happens when we define a bean within an ApplicationContext which has the same name as another bean. It works by simply replacing the former bean in case of a name conflict.

What should I do if I encounter a bean name conflict?

In some cases, it's possible to encounter a name conflict caused by beans originating from 3rd party spring-supported libraries. When this happens, we should attempt to identify which conflicting bean belongs to our application, to determine if any of the above solutions can be used.


1 Answers

Finally I was able to solve this issue. You cant give jobName and className exactly same like employeesJob(). I changed the Job Name and its working fine now.

like image 185
Jeff Cook Avatar answered Oct 07 '22 20:10

Jeff Cook