package cokr.xit.ens.core.config.support;

import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.lang.NonNull;

/**
 *
 * <pre>
 *  API v1, v2 등으로 분류하는 경우
 *  Bean 이름 식별시 풀패키지 명으로 식별 하도록 함
 *  Use : 아래 두번째 방식 권장 - 첫 번째 방식은 재확인 필요
 *
 *     -------------------------
 *     Application.java
 *
 *     SpringApplicationBuilder applicationBuilder = new SpringApplicationBuilder(Application.class);
 *     CustomBeanNameGenerator beanNameGenerator = new CustomBeanNameGenerator();
 *     applicationBuilder.beanNameGenerator(beanNameGenerator);
 *     applicationBuilder.build().run(args);
 *     -------------------------
 *
 *     OR
 *
 *     -------------------------
 *     @ComponentScan(
 *         nameGenerator = CustomBeanNameGenerator.class,
 *         basePackages = {"cokr.xit.biz", "cokr.xit.core"},
 *         excludeFilters = @ComponentScan.Filter(
 *                 type = FilterType.ASPECTJ,
 *                 pattern = {
 *                         "cokr.xit"
 *                 }
 *         )
 *     )
 *
 *     DatabaseConfig.java
 *     @MapperScan(
 *         nameGenerator = CustomBeanNameGenerator.class,
 *         basePackages = DataSourceConfig.PACKAGE,
 *         sqlSessionFactoryRef = "sqlSessionFactory"
 *     )
 *     static final String PACKAGE = "cokr.xit.**.mapper";
 *     --------------------------
 *
 * </pre>
 *
 */
public class CustomBeanNameGenerator implements BeanNameGenerator {
    @Override
    public @NonNull String generateBeanName(@NonNull BeanDefinition definition, @NonNull BeanDefinitionRegistry registry) {
        final String result = generateFullBeanName((AnnotatedBeanDefinition)definition);
        return result;
    }

    private String generateFullBeanName(final AnnotatedBeanDefinition definition) {
        return definition.getMetadata().getClassName();
    }
}