๐ Study
A bean with that name has already been defined in XXX defined in @EnableElasticsearchRepositories declared on ElasticsearchConfig and overriding is disabled.
ย
ย
๋ฌธ์ ์ํฉ
Spring Boot ํ๊ฒฝ์์ spring-data-elasticsearch ์ฐ๋์ ํ๋ ์ค ์๋์ ๊ฐ์ ์๋ฌ๊ฐ ๋ฐ์ํ๋ค.
ํด์ํ์๋ฉด elasticSuggestionRepository ๋น์ด ์ด๋ฏธ ๋ฑ๋ก๋์ด ์๊ธฐ ๋๋ฌธ์ ์ค๋ฒ๋ผ์ด๋ฉํ์ฌ ๋ฑ๋กํ ์ ์๋ค๋ ๊ฒ์ด์๋ค.
Description: The bean 'elasticSuggestionRepository', defined in com.dreamypatisiel.devdevdev.elastic.domain.repository.ElasticSuggestionRepository defined in @EnableJpaRepositories declared on DevdevdevApplication, could not be registered. A bean with that name has already been defined in com.dreamypatisiel.devdevdev.elastic.domain.repository.ElasticSuggestionRepository defined in @EnableElasticsearchRepositories declared on ElasticsearchConfig and overriding is disabled.
ย
ํด๋น ๋น์ด ์ด๋์ ๋ฑ๋ก๋๊ณ ์๋์ง ์ถ์ ํ๊ธฐ ์ํ์ฌ ์ฐ์ ํจํค์ง ๊ตฌ์กฐ๋ฅผ ์ ๋ฆฌํ๊ณ ๊ด๋ จ ํด๋์ค๋ฅผ ์ดํด๋ณด์๋ค.
// ํจํค์ง ๊ตฌ์กฐ elastic ใดconfig ใดElasticsearchConfig.java ใดdomain ใดdocument ใดElasticSuggestion.java ใดrepository ใดElasticSuggestionRepository.java
ย
xxxApplication.java
@SpringBootApplication public class DevdevdevApplication { public static void main(String[] args) { SpringApplication.run(DevdevdevApplication.class, args); } }
ย
ElasticsearchConfig.java
@Configuration @EnableElasticsearchRepositories(basePackages = {"com.dreamypatisiel.devdevdev.elastic.domain.repository"}) public class ElasticsearchConfig extends AbstractElasticsearchConfiguration { @Value("${elasticsearch.host}") private String host; @Value("${elasticsearch.port}") private String port; @Override public @NonNull RestHighLevelClient elasticsearchClient() { ClientConfiguration clientConfiguration = ClientConfiguration.builder() .connectedTo(host + ":" + port) .build(); return RestClients.create(clientConfiguration).rest(); } @Bean public ElasticsearchOperations elasticsearchOperations(RestHighLevelClient client) { return new ElasticsearchRestTemplate(client); } }
์ configํ์ผ์
@EnableElasticsearchRepositories
๋ก ์ธํด ๋น์ด ์ค๋ณต ๋ฑ๋ก๋๊ณ ์๋ ๊ฒ์ผ๋ก ๋ณด์๋ค.SpringBoot ์์๋ ์๋์ผ๋ก @EnableJpaRepositories๊ฐ ์ค์ ๋๋๋ฐ, ์ด๋ ๋ฑ๋ก๋ ๋น์ด
@EnableElasticsearchRepositories
์ ์ํด ๋ ์ค์บ๋๊ณ ๋ฑ๋ก๋ ๋ ๋ฐ์ํ๋ ๋ฌธ์ ๊ฐ ์๋๊น ์ถ์๋ค.ย
ย
๋ annotation์ ์ดํด๋ณด๋ฉด ๋ค์๊ณผ ๊ฐ์ด
xxxRepositoriesRegistrar
๋ฅผ importํ๊ณ ์๊ณ , ๊ฐ ํด๋์ค๋ ๋ชจ๋ ๊ณตํต์ผ๋ก ReposistoryBeanDefinitionRegistrarSupport
๋ฅผ ์์๋ฐ๊ณ ์๋ค.JpaRepositoriesRegistrar.class
ElasticsearchRepositoruesRegistrar.class
ย
์ฆ, @EnableElasticsearchRepositories โ
ReposistoryBeanDefinitionRegistrarSupport
์ ์ํด ๋ฑ๋ก๋ ๋น์ด @EnableJpaRepositories โ ReposistoryBeanDefinitionRegistrarSupport
์ ์ํด ๋ ๋ฑ๋ก๋๊ธฐ ๋๋ฌธ์ ์ค๋ณต ๋ฌธ์ ๊ฐ ๋ฐ์ํ๋ ๊ฒ์ผ๋ก ๋ณด์ธ๋ค.ย
ํด๊ฒฐ
๋น์ด ๋ ๋ฒ ๋ฑ๋ก๋์ด ์๋ฌ๊ฐ ๋ฐ์ํ๋ ๊ฒ์ด๋ฏ๋ก, ๋น ๋ฑ๋ก์ด ํ ๋ฒ๋ง ๋๋๋ก ๋ณ๊ฒฝํ์ฌ ํด๊ฒฐํ๋ค!
@EnableJpaRepositories(excludeFilters = @ComponentScan.Filter( type = FilterType.ASSIGNABLE_TYPE, classes = ElasticSuggestionRepository.class)) @ConfigurationPropertiesScan @EnableJpaAuditing @SpringBootApplication public class DevdevdevApplication {}
@Configuration @EnableElasticsearchRepositories(basePackages = {"com.dreamypatisiel.devdevdev.elastic.domain.repository"}) public class ElasticsearchConfig extends AbstractElasticsearchConfiguration {}
ย
ย
ย