1

In Spring (Boot 2), is there a way to register an anonymous class to the Bean Factory inside a method? (Register as a Component for example )

Example: I want to to register "taskletInstance" as a Component so it's aware of the Spring Context. I can achieve this by taking it outside the method and annotate as @Bean. So I have 2 Bean annotated methods. But I am curious if I can do it inside the updateStep method:

@Bean
@Qualifier("myStep")
public Step handleStep() {

    Tasklet taskletInstance = new AbstractTasklet() {
        @Override
        public Data handleData(Data data) {
            //handle data
            return data;
        }
    };

    return stepBuilderFactory.get("handleStep").tasklet(taskletInstance).build();
}
Spring
  • 11,333
  • 29
  • 116
  • 185
  • Possible duplicate of [How to add bean programmatically to Spring context?](https://stackoverflow.com/questions/43021493/how-to-add-bean-programmatically-to-spring-context) – Adam Siemion Oct 09 '19 at 14:30
  • Are you getting any error if you are using the above bean? – Sujay Mohan Oct 09 '19 at 16:45

0 Answers0