Assigning Bean Annotations to Non-Working Methods in Trivial Spring Sequence Example

I am familiar with the whole Spring stack.

I mean a trivial Spring Batch example hosted at spring.io: https://spring.io/guides/gs/batch-processing/

Each method in the job configuration class is BatchConfiguration

annotated with @Bean

. Except for the setter, importUserJob

is there any point in the bean's bean annotation of helper methods called by the bean of the same type that creates the job?

In my opinion, by removing @Bean

annotations from all methods except importUserJob

, all this code will be called only once per Spring instance and only from the method importUserJob

.

All Spring Batch examples are shown annotated @Bean

around non-working methods, so there must be something that eludes me.

Cut excerpt below:

@Configuration
@EnableBatchProcessing
public class BatchConfiguration {
    @Autowired public JobBuilderFactory jobBuilderFactory;
    @Autowired public StepBuilderFactory stepBuilderFactory;
    @Autowired public DataSource dataSource;

    @Bean
    public FlatFileItemReader<Person> reader() ...

    @Bean
    public PersonItemProcessor processor() ...

    @Bean
    public JdbcBatchItemWriter<Person> writer() ...

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

    @Bean
    public Step step1() ...
}

      

Thanks for reading and helping.

+3


source to share


2 answers


Technically you can write a non-use package @Bean

for the functions you mentioned and your assumptions are correct since beans are not used elsewhere. (I haven't tried it myself, but I think it will work - unless Spring Batch uses them for other things).

A @Bean

annotated object gets its lifecycle handled by Spring so that it can properly perform dependency injection. This way, you can decouple the construction of objects from their use (IoC). Especially when you use autoloading, dependency injection becomes much easier.

You can of course do it manually, but it will require a lot of template code and you will have to submit customized objects. Your example might not have any advantage in creating beans for everyone, but as the package gets bigger, I'm sure you'll miss out on the flexibility of handling the Springs bean. I personally wrote a batch with some other frameworks without Springs dependency injection. At first I had a compact model, but as it grew, I really felt the pain of template code that made my code readable.



In addition, Spring managed beans do other things like. closing resources.

Of course, Dependency Injection is just a tool and might be overkill for certain use cases. These articles may interest you:

+1


source


The annotation @Bean

makes the methods create bean methods, and the resulting object will be registered with Spring ApplicationContext

as a bean.

As a result, it will participate in the lifecycle of the specified context, and it will receive callbacks such as annotated methods @PostConstruct

, or interface callbacks such as InitializingBean

.



If you omit @Bean

it doesn't happen. For example, for FlatFileItemReader, this will result in a method call afterPropertiesSet

that will result in an incompletely initialized bean.

Therefore, @Bean

it should not be omitted as it @Bean

is the <bean />

XML equivalent .

0


source







All Articles