From 3e21f26348d82fed8d701e730629a572fb210065 Mon Sep 17 00:00:00 2001 From: minuk926 Date: Thu, 26 May 2022 18:06:58 +0900 Subject: [PATCH] =?UTF-8?q?config=20:=20gradle=20com.ewerk.gradle.plugins.?= =?UTF-8?q?querydsl=20plugin=20=EC=A0=9C=EA=B1=B0=20=20=20=20=20=20=20=20?= =?UTF-8?q?=20=20Gradle=20Annotation=20processor=EB=A5=BC=20=EC=82=AC?= =?UTF-8?q?=EC=9A=A9=ED=95=98=EB=8F=84=EB=A1=9D=20=EC=84=A4=EC=A0=95=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/xit/Application.java | 9 +-- .../_ignore/CustomBeanNameGenerator.java | 56 +++++++++++++++++++ .../_ignore/CustomBeanNameGenerator2.java | 54 ++++++++++++++++++ .../config/database/DataSourceConfig.java | 2 + .../support/CustomBeanNameGenerator.java | 45 +++------------ 5 files changed, 124 insertions(+), 42 deletions(-) create mode 100644 src/main/java/com/xit/core/config/_ignore/CustomBeanNameGenerator.java create mode 100644 src/main/java/com/xit/core/config/_ignore/CustomBeanNameGenerator2.java 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