mirror of
https://gitee.com/easii/mapstruct-plus.git
synced 2025-12-07 17:48:35 +08:00
- 优化依赖注入
- 修复部分场景的问题
This commit is contained in:
parent
6bc4a6c16b
commit
b1aacc7a2d
@ -572,11 +572,18 @@ public class AutoMapperProcessor extends AbstractProcessor {
|
||||
).filter(Objects::nonNull).flatMap(Collection::stream).collect(Collectors.toList());
|
||||
|
||||
if (CollectionUtils.isNotEmpty(dependencyMappers)) {
|
||||
List<ClassName> uses = Optional.ofNullable(metadata.getUsesClassNameList()).orElse(new ArrayList<>());
|
||||
uses.addAll(dependencyMappers);
|
||||
metadata.setUsesClassNameList(uses);
|
||||
metadata.addUseList(dependencyMappers);
|
||||
}
|
||||
}
|
||||
// source
|
||||
List<ClassName> sourceDependencies =
|
||||
typeRelationMappers.get(metadata.getSourceClassName().reflectionName());
|
||||
|
||||
if (CollectionUtils.isNotEmpty(sourceDependencies)) {
|
||||
sourceDependencies.removeIf(
|
||||
sourceDependency -> sourceDependency.reflectionName().equals(metadata.mapperName()));
|
||||
metadata.addUseList(sourceDependencies);
|
||||
}
|
||||
});
|
||||
|
||||
mapperList.forEach(this::writeAutoMapperClassFile);
|
||||
|
||||
@ -4,6 +4,7 @@ import com.squareup.javapoet.ClassName;
|
||||
import com.squareup.javapoet.TypeName;
|
||||
import io.github.linpeilie.processor.utils.MapperUtils;
|
||||
import io.github.linpeilie.utils.StrUtil;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import org.mapstruct.NullValueMappingStrategy;
|
||||
@ -73,6 +74,13 @@ public class AutoMapperMetadata extends AbstractMapperMetadata {
|
||||
return mapperPackage() + "." + mapperName();
|
||||
}
|
||||
|
||||
public boolean addUseList(List<ClassName> uses) {
|
||||
if (this.usesClassNameList == null) {
|
||||
this.usesClassNameList = new ArrayList<>();
|
||||
}
|
||||
return usesClassNameList.addAll(uses);
|
||||
}
|
||||
|
||||
/*************** getter/setter ***************/
|
||||
|
||||
public String mapperName() {
|
||||
@ -83,10 +91,6 @@ public class AutoMapperMetadata extends AbstractMapperMetadata {
|
||||
this.mapperName = mapperName;
|
||||
}
|
||||
|
||||
public String getMapperName() {
|
||||
return mapperName;
|
||||
}
|
||||
|
||||
public String getMapperNameSuffix() {
|
||||
return mapperNameSuffix;
|
||||
}
|
||||
|
||||
@ -1,2 +1,2 @@
|
||||
<#-- @ftlvariable name="" type="io.github.linpeilie.processor.enhance.model.SpringDelayInjectMapperReference" -->
|
||||
private <@includeModel object=type/> ${variableName} = SpringContextUtils.getBean(<@includeModel object=type/>.class);
|
||||
private <@includeModel object=type/> ${variableName} = SpringContextUtils.getBean("${variableName}", <@includeModel object=type/>.class);
|
||||
@ -1,20 +1,68 @@
|
||||
package io.github.linpeilie.mapstruct;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.ListableBeanFactory;
|
||||
import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
|
||||
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
|
||||
public class SpringContextUtils implements ApplicationContextAware {
|
||||
public class SpringContextUtils implements BeanFactoryPostProcessor, ApplicationContextAware {
|
||||
|
||||
private static ConfigurableListableBeanFactory beanFactory;
|
||||
private static ApplicationContext applicationContext;
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||
public SpringContextUtils() {
|
||||
}
|
||||
|
||||
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
|
||||
SpringContextUtils.beanFactory = beanFactory;
|
||||
}
|
||||
|
||||
public void setApplicationContext(ApplicationContext applicationContext) {
|
||||
SpringContextUtils.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
public static ApplicationContext getApplicationContext() {
|
||||
return applicationContext;
|
||||
}
|
||||
|
||||
public static ListableBeanFactory getBeanFactory() {
|
||||
ListableBeanFactory factory = null == beanFactory ? applicationContext : beanFactory;
|
||||
if (null == factory) {
|
||||
throw new RuntimeException("No ConfigurableListableBeanFactory or ApplicationContext injected, maybe not in the Spring environment?");
|
||||
} else {
|
||||
return (ListableBeanFactory)factory;
|
||||
}
|
||||
}
|
||||
|
||||
public static ConfigurableListableBeanFactory getConfigurableBeanFactory() {
|
||||
ConfigurableListableBeanFactory factory;
|
||||
if (null != beanFactory) {
|
||||
factory = beanFactory;
|
||||
} else {
|
||||
if (!(applicationContext instanceof ConfigurableApplicationContext)) {
|
||||
throw new RuntimeException("No ConfigurableListableBeanFactory from context!");
|
||||
}
|
||||
|
||||
factory = ((ConfigurableApplicationContext)applicationContext).getBeanFactory();
|
||||
}
|
||||
|
||||
return factory;
|
||||
}
|
||||
|
||||
public static <T> T getBean(Class<T> clazz) {
|
||||
return applicationContext.getBean(clazz);
|
||||
return getBeanFactory().getBean(clazz);
|
||||
}
|
||||
|
||||
public static <T> T getBean(String name, Class<T> clazz) {
|
||||
try {
|
||||
return getBean(clazz);
|
||||
} catch (NoUniqueBeanDefinitionException e) {
|
||||
return getBeanFactory().getBean(name, clazz);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user