mirror of
https://gitee.com/easii/mapstruct-plus.git
synced 2025-12-06 17:18:43 +08:00
增加Spring延迟加载的实现
This commit is contained in:
parent
6f933585db
commit
9ee817b2a1
@ -65,7 +65,8 @@ public interface ContextConstants {
|
||||
|
||||
interface ComponentModelConfig {
|
||||
String qualifiedClassName = "io.github.linpeilie.annotations.ComponentModelConfig";
|
||||
String defaultComponentModel = MappingConstants.ComponentModel.SPRING;
|
||||
String springLazy = "spring-lazy";
|
||||
String defaultComponentModel = springLazy;
|
||||
}
|
||||
|
||||
interface ConvertAdapter {
|
||||
|
||||
@ -0,0 +1,25 @@
|
||||
package io.github.linpeilie.processor.enhance.model;
|
||||
|
||||
import io.github.linpeilie.processor.enhance.processor.SpringComponentProcessor;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import org.mapstruct.ap.internal.model.MapperReference;
|
||||
import org.mapstruct.ap.internal.model.common.Type;
|
||||
|
||||
public class SpringDelayInjectMapperReference extends MapperReference {
|
||||
|
||||
private final Type springContextUtil;
|
||||
|
||||
public SpringDelayInjectMapperReference(Type type, String variableName, boolean isUsed, Type springContextUtil) {
|
||||
super(type, variableName, isUsed);
|
||||
this.springContextUtil = springContextUtil;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Type> getImportTypes() {
|
||||
Set<Type> importTypes = new HashSet<>();
|
||||
importTypes.add(getType());
|
||||
importTypes.add(springContextUtil);
|
||||
return importTypes;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,48 @@
|
||||
package io.github.linpeilie.processor.enhance.processor;
|
||||
|
||||
import io.github.linpeilie.processor.ContextConstants;
|
||||
import io.github.linpeilie.processor.enhance.model.SpringDelayInjectMapperReference;
|
||||
import io.github.linpeilie.utils.CollectionUtils;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import org.mapstruct.ap.internal.gem.InjectionStrategyGem;
|
||||
import org.mapstruct.ap.internal.model.Annotation;
|
||||
import org.mapstruct.ap.internal.model.Field;
|
||||
import org.mapstruct.ap.internal.model.Mapper;
|
||||
import org.mapstruct.ap.internal.processor.AnnotationBasedComponentModelProcessor;
|
||||
|
||||
public class SpringComponentProcessor extends AnnotationBasedComponentModelProcessor {
|
||||
|
||||
private Annotation component() {
|
||||
return new Annotation(getTypeFactory().getType("org.springframework.stereotype.Component"));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getComponentModelIdentifier() {
|
||||
return ContextConstants.ComponentModelConfig.springLazy;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<Annotation> getTypeAnnotations(Mapper mapper) {
|
||||
return CollectionUtils.newArrayList(component());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<Annotation> getMapperReferenceAnnotations() {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean requiresGenerationOfDecoratorClass() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Field replacementMapperReference(Field originalReference,
|
||||
List<Annotation> annotations,
|
||||
InjectionStrategyGem injectionStrategy) {
|
||||
return new SpringDelayInjectMapperReference(originalReference.getType(), originalReference.getVariableName(),
|
||||
originalReference.isUsed(),
|
||||
getTypeFactory().getType("io.github.linpeilie.mapstruct.SpringContextUtils"));
|
||||
}
|
||||
}
|
||||
@ -12,3 +12,4 @@ org.mapstruct.ap.internal.processor.MethodRetrievalProcessor
|
||||
org.mapstruct.ap.internal.processor.SpringComponentProcessor
|
||||
org.mapstruct.ap.internal.processor.MapperServiceProcessor
|
||||
io.github.linpeilie.processor.solon.SolonComponentProcessor
|
||||
io.github.linpeilie.processor.enhance.processor.SpringComponentProcessor
|
||||
|
||||
@ -0,0 +1,2 @@
|
||||
<#-- @ftlvariable name="" type="io.github.linpeilie.processor.enhance.model.SpringDelayInjectMapperReference" -->
|
||||
private <@includeModel object=type/> ${variableName} = SpringContextUtils.getBean(<@includeModel object=type/>.class);
|
||||
@ -24,4 +24,9 @@ public class MapstructAutoConfiguration {
|
||||
return new Converter(converterFactory);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SpringContextUtils springContextUtils() {
|
||||
return new SpringContextUtils();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -0,0 +1,20 @@
|
||||
package io.github.linpeilie.mapstruct;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
|
||||
public class SpringContextUtils implements ApplicationContextAware {
|
||||
|
||||
private static ApplicationContext applicationContext;
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||
SpringContextUtils.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
public static <T> T getBean(Class<T> clazz) {
|
||||
return applicationContext.getBean(clazz);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user