diff --git a/src/main/java/com/xit/Application.java b/src/main/java/com/xit/Application.java index 78457df..5a4c4ec 100644 --- a/src/main/java/com/xit/Application.java +++ b/src/main/java/com/xit/Application.java @@ -35,11 +35,12 @@ import java.io.IOException; AppProperties.class }) @ComponentScan( + nameGenerator = CustomBeanNameGenerator.class, basePackages = {"com.xit.biz", "com.xit.core"}, excludeFilters = @ComponentScan.Filter( type = FilterType.ASPECTJ, pattern = { - "com.xit.._ignore..*" + "com.xit" //"com..support.auth.jwt..*" } ) @@ -66,9 +67,9 @@ public class Application { //WebApplicationInitializer { // beanName Generator 등록 : API v1, v2 등으로 분류하는 경우 // Bean 이름 식별시 풀패키지 명으로 식별 하도록 함 - CustomBeanNameGenerator beanNameGenerator = new CustomBeanNameGenerator(); - beanNameGenerator.addBasePackages(BEAN_GEN_BASE_PACKAGE); - applicationBuilder.beanNameGenerator(beanNameGenerator); + //CustomBeanNameGenerator beanNameGenerator = new CustomBeanNameGenerator(); + //beanNameGenerator.addBasePackages(BEAN_GEN_BASE_PACKAGE); + //applicationBuilder.beanNameGenerator(beanNameGenerator); //TODO : 이벤트 실행 시점이 Application context 실행 이전인 경우 리스너 추가 //application.listeners(new xitCoreApplicationListner()); diff --git a/src/main/java/com/xit/core/config/_ignore/CustomBeanNameGenerator.java b/src/main/java/com/xit/core/config/_ignore/CustomBeanNameGenerator.java new file mode 100644 index 0000000..0271898 --- /dev/null +++ b/src/main/java/com/xit/core/config/_ignore/CustomBeanNameGenerator.java @@ -0,0 +1,56 @@ +package com.xit.core.config._ignore; + +import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.beans.factory.support.BeanDefinitionRegistry; +import org.springframework.beans.factory.support.BeanNameGenerator; +import org.springframework.context.annotation.AnnotationBeanNameGenerator; +import org.springframework.lang.NonNull; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * 패키지 이름이 다른데, 클래스 이름이 동일한 경우 + * 두 개의 클래스 모두 빈으로 등록 + * + * 특정 어노테이션을 가진 클래스만 패키지 이름까지 포함하여 빈 등록 시에 이름을 구분할 수 있도록 한다. + * + * /v1/api, /v2/api 형태로 생성 가능하도록 + */ +public class CustomBeanNameGenerator implements BeanNameGenerator { + + /** + * basePackages 외에 scaning된 beanGenerator + */ + private static final BeanNameGenerator DELEGATE = new AnnotationBeanNameGenerator(); + + /** + * VersioningBeanNameGenerator 대상 package 경로 + */ + private final List basePackages = new ArrayList<>( + Arrays.asList("com.xit.biz", "com.xit.core") + ); + + @Override + public @NonNull String generateBeanName(@NonNull BeanDefinition definition, @NonNull BeanDefinitionRegistry registry) { + if(isTargetPackageBean(definition)) { + return getBeanName(definition); + } + + return DELEGATE.generateBeanName(definition, registry); + } + + private boolean isTargetPackageBean(BeanDefinition definition) { + String beanClassName = getBeanName(definition); + return basePackages.stream().anyMatch(beanClassName::startsWith); + } + + private String getBeanName(BeanDefinition definition) { + return definition.getBeanClassName(); + } + + public void addBasePackages(String path) { + this.basePackages.add(path); + } +} \ No newline at end of file diff --git a/src/main/java/com/xit/core/config/_ignore/CustomBeanNameGenerator2.java b/src/main/java/com/xit/core/config/_ignore/CustomBeanNameGenerator2.java new file mode 100644 index 0000000..bf0c814 --- /dev/null +++ b/src/main/java/com/xit/core/config/_ignore/CustomBeanNameGenerator2.java @@ -0,0 +1,54 @@ +package com.xit.core.config._ignore; + +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.context.annotation.AnnotationBeanNameGenerator; +import org.springframework.lang.NonNull; +import org.springframework.stereotype.Service; + +import java.util.Set; + +/** + *
+ * 패키지 이름이 다른데, 클래스 이름이 동일한 경우
+ * 두 개의 클래스 모두 빈으로 등록
+ *
+ * 특정 어노테이션을 가진 클래스만 패키지 이름까지 포함하여 빈 등록 시에 이름을 구분할 수 있도록 한다.
+ *
+ * /v1/api, /v2/api 형태로 생성 가능하도록
+ * 
+ */ +public class CustomBeanNameGenerator2 implements BeanNameGenerator { + + private final AnnotationBeanNameGenerator defaultNameGenerator = new AnnotationBeanNameGenerator(); + + @Override + public @NonNull String generateBeanName(@NonNull BeanDefinition definition, @NonNull BeanDefinitionRegistry registry) { + String beanName; + + if (isService(definition)) { + beanName = getFullName((AnnotatedBeanDefinition) definition); + } else { + beanName = defaultNameGenerator.generateBeanName(definition, registry); + } + return beanName; + } + + private String getFullName(final AnnotatedBeanDefinition definition) { + // 패키지를 포함한 전체 이름을 반환한다. + return definition.getMetadata().getClassName(); + } + + private boolean isService(final BeanDefinition definition) { + if (definition instanceof AnnotatedBeanDefinition) { + final Set annotationTypes = ((AnnotatedBeanDefinition) definition).getMetadata() + .getAnnotationTypes(); + + return annotationTypes.stream() + .anyMatch(type -> type.equals(Service.class.getName())); + } + return false; + } +} \ No newline at end of file diff --git a/src/main/java/com/xit/core/config/database/DataSourceConfig.java b/src/main/java/com/xit/core/config/database/DataSourceConfig.java index 9e439fc..4fe852b 100644 --- a/src/main/java/com/xit/core/config/database/DataSourceConfig.java +++ b/src/main/java/com/xit/core/config/database/DataSourceConfig.java @@ -1,6 +1,7 @@ package com.xit.core.config.database; import com.fasterxml.jackson.datatype.hibernate5.Hibernate5Module; +import com.xit.core.config.support.CustomBeanNameGenerator; import com.xit.core.support.CamelCaseLinkedMap; import com.xit.core.support.CamelCaseMap; import com.xit.core.support.ObjectTypeHandler; @@ -54,6 +55,7 @@ import java.util.Map; ) // Mybatis @MapperScan( + nameGenerator = CustomBeanNameGenerator.class, basePackages = DataSourceConfig.PACKAGE, sqlSessionFactoryRef = "sqlSessionFactory" ) diff --git a/src/main/java/com/xit/core/config/support/CustomBeanNameGenerator.java b/src/main/java/com/xit/core/config/support/CustomBeanNameGenerator.java index 218e40c..16d38c1 100644 --- a/src/main/java/com/xit/core/config/support/CustomBeanNameGenerator.java +++ b/src/main/java/com/xit/core/config/support/CustomBeanNameGenerator.java @@ -1,52 +1,21 @@ package com.xit.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.context.annotation.AnnotationBeanNameGenerator; import org.springframework.lang.NonNull; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -/** - * Bean 이름 식별시 패키지를 포함하도록 지정 - * /v1/api, /v2/api 형태로 생성 가능하도록 - */ public class CustomBeanNameGenerator implements BeanNameGenerator { - - /** - * basePackages 외에 scaning된 beanGenerator - */ - private static final BeanNameGenerator DELEGATE = new AnnotationBeanNameGenerator(); - - /** - * VersioningBeanNameGenerator 대상 package 경로 - */ - private final List basePackages = new ArrayList<>( - Arrays.asList("com.xit.biz", "com.xit.core") - ); - @Override public @NonNull String generateBeanName(@NonNull BeanDefinition definition, @NonNull BeanDefinitionRegistry registry) { - if(isTargetPackageBean(definition)) { - return getBeanName(definition); - } - - return DELEGATE.generateBeanName(definition, registry); - } - - private boolean isTargetPackageBean(BeanDefinition definition) { - String beanClassName = getBeanName(definition); - return basePackages.stream().anyMatch(beanClassName::startsWith); - } - - private String getBeanName(BeanDefinition definition) { - return definition.getBeanClassName(); + final String result; + result = generateFullBeanName((AnnotatedBeanDefinition)definition); + System.out.println(result); + return result; } - public void addBasePackages(String path) { - this.basePackages.add(path); + private String generateFullBeanName(final AnnotatedBeanDefinition definition) { + return definition.getMetadata().getClassName(); } } \ No newline at end of file