티스토리 뷰

Programming/Spring

[주의] DI 우선 순위

Albothyl 2019. 6. 27. 21:40

SomeJpaRepositoryConfig, OtherJpaRepositoryConfig 2개의 Config가 존재한다. 이 경우 SomeJpaRepositoryConfig에 있는 @Primary Annotation으로 인해 OtherJpaRepositoryConfig의 DataSource와 EntityFactory는 SomeJpaRepository의 DataSource와 EntityFactory가 주입된다.

@Configuration
public class SomeJpaRepositoryConfig {
	@Bean
	@Primary
	public DataSource dataSource() {
		...
		return new DataSource(...);
	}

	@Bean
	@Primary
	public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) {
		return new JpaEntityManagerFactoryBeanBuilder(environment)
			.setDataSource(dataSource)
			.build();
	}

	@Bean
	@Primary
	public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
		return new JpaTransactionManagerBuilder(environment)
			.setEntityManagerFactory(entityManagerFactory)
			.build();
	}
}

// --------------------------------------------------------

@Configuration
public class OtherJpaRepositoryConfig {
	@Bean
	public DataSource otherDataSource() {
		...
		return new DataSource(...);
	}

	@Bean
	public LocalContainerEntityManagerFactoryBean otherEntityManagerFactory(DataSource otherDataSource) {
		return new JpaEntityManagerFactoryBeanBuilder(...)
			.setDataSource(otherDataSource)
			.build();
	}

	@Bean
	public PlatformTransactionManager transactionManager(EntityManagerFactory otherEntityManagerFactory) {
		return new JpaTransactionManagerBuilder(...)
			.setEntityManagerFactory(otherEntityManagerFactory)
			.build();
	}
}

이 문제를 해결하기 위해서는 명시적으로 @Qualifier Annotation을 추가하여 주입받을 Bean을 명시한다.

@Configuration
public class OtherJpaRepositoryConfig {
	@Bean
	public DataSource otherDataSource() {
		...
		return new DataSource(...);
	}

	@Bean
	public LocalContainerEntityManagerFactoryBean otherEntityManagerFactory(@Qualifier("otherDataSource") DataSource otherDataSource) {
		return new JpaEntityManagerFactoryBeanBuilder(...)
			.setDataSource(otherDataSource)
			.build();
	}

	@Bean
	public PlatformTransactionManager transactionManager(@Qualifier("otherEntityManagerFactory") EntityManagerFactory otherEntityManagerFactory) {
		return new JpaTransactionManagerBuilder(...)
			.setEntityManagerFactory(otherEntityManagerFactory)
			.build();
	}
}

다중 DataSource 설정에서 DataSource, EntityFactory 등의 Bean을 생성하고, 주입할 때 DI 우선순위로 인한 문제가 발생할 수 있다. @Primary 등의 Annotation을 확인하지 않고, 설정을 진행할 경우 의도한 Bean이 아닌 다른 Bean이 주입될 수 있다. 에러도 없기 때문에 항상 배포후에는 간단한 Sanity Test를 진행하는 습관이 필요하다.


이 예제로 볼 때 DI 주입 순서는 아래와 같다.

1. @Qualifier

2. @Primary

3. @Autowired: Type

4. @Autowired: Name

 

 

 


 

'Programming > Spring' 카테고리의 다른 글

[주의] Controller Parameter Mapping Exception  (0) 2019.07.09
[주의] Spring MVC Redirect 와 Out Of Memory  (0) 2019.07.09
SmartLifecycle  (0) 2019.06.10
JDBC Template  (0) 2019.05.05
Enhanced Registrar  (0) 2019.04.12
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함