[JAVA] @BatchDataSource used when defining multiple DataSources in spring-batch

When defining multiple DataSources in spring-batch, you can specify which one to use in auto-configuration of spring-batch with `` @ BatchDataSource```. According to the javadoc of ``` @ BatchDataSource```, it seems to have been added in `2.2.0```.

The definition is as follows, and the reality is just `@ Qualifier```. According to javadoc, the typical usage is a main DataSource that gives `@ Primary``` and a second DataSource for spring-batch that gives this annotation. It seems that this is used when the main DB and the metadata storage destination DB are separated.

/**
 * Qualifier annotation for a DataSource to be injected into Batch auto-configuration. Can
 * be used on a secondary data source, if there is another one marked as
 * {@link Primary @Primary}.
 *
 * @author Dmytro Nosan
 * @since 2.2.0
 */
@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE, ElementType.ANNOTATION_TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Qualifier
public @interface BatchDataSource {

}

Example of use.

	@Bean
	@BatchDataSource
	public DataSource springBatchDs() {
		return DataSourceBuilder.create().url("jdbc:h2:~/test").username("sa").password("").build();
	}
	
	@Bean
	@Primary
	public DataSource primaryDs() {
		return DataSourceBuilder.create().url("jdbc:oracle:thin:system/oracle@localhost:11521/XEPDB1").username("system").password("oracle").build();
	}

In the case of the above example, the main is Oracle and the metadata storage destination is H2.

It's not that this kind of setting wasn't possible in the past, but it seems that the dedicated annotation makes it easier to clarify the intention.

Recommended Posts

@BatchDataSource used when defining multiple DataSources in spring-batch
When seeking multiple in a Java array
When seeking multiple in a Java array
@BatchDataSource used when defining multiple DataSources in spring-batch
Add an item when logging in with devise
How to define multiple orm.xml in Spring4, JPA2.1