mirror of
https://gitee.com/easii/mapstruct-plus.git
synced 2025-12-06 09:08:54 +08:00
增加用例
修改 SpringContextUtils 类名为 SpringContextUtils4Msp
This commit is contained in:
parent
64c2ffc645
commit
e01776d00c
@ -0,0 +1,15 @@
|
||||
package io.github.linpeilie.me.annotation;
|
||||
|
||||
import io.github.linpeilie.annotations.AutoMapper;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@CreateMapper
|
||||
@AutoMapper(target = CarDTO.class)
|
||||
public class CarCreate {
|
||||
|
||||
private String name;
|
||||
|
||||
private String operatorUserId;
|
||||
|
||||
}
|
||||
@ -0,0 +1,14 @@
|
||||
package io.github.linpeilie.me.annotation;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class CarDTO {
|
||||
|
||||
private String name;
|
||||
|
||||
private String createBy;
|
||||
|
||||
private String updateBy;
|
||||
|
||||
}
|
||||
@ -0,0 +1,15 @@
|
||||
package io.github.linpeilie.me.annotation;
|
||||
|
||||
import io.github.linpeilie.annotations.AutoMapper;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@UpdateMapper
|
||||
@AutoMapper(target = CarDTO.class)
|
||||
public class CarUpdate {
|
||||
|
||||
private String name;
|
||||
|
||||
private String operatorUserId;
|
||||
|
||||
}
|
||||
@ -0,0 +1,7 @@
|
||||
package io.github.linpeilie.me.annotation;
|
||||
|
||||
import io.github.linpeilie.annotations.AutoMapping;
|
||||
|
||||
@AutoMapping(source = "operatorUserId", target = "createBy")
|
||||
public @interface CreateMapper {
|
||||
}
|
||||
@ -0,0 +1,8 @@
|
||||
package io.github.linpeilie.me.annotation;
|
||||
|
||||
import io.github.linpeilie.annotations.AutoMapping;
|
||||
|
||||
@AutoMapping(source = "operatorUserId", target = "updateBy")
|
||||
public @interface UpdateMapper {
|
||||
|
||||
}
|
||||
@ -0,0 +1,53 @@
|
||||
package io.github.linpeilie;
|
||||
|
||||
import io.github.linpeilie.me.annotation.CarCreate;
|
||||
import io.github.linpeilie.me.annotation.CarDTO;
|
||||
import io.github.linpeilie.me.annotation.CarUpdate;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
@Slf4j
|
||||
@SpringBootTest(classes = Application.class)
|
||||
public class AutoMappingAnnotationTest {
|
||||
|
||||
@Autowired
|
||||
private Converter converter;
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
CarCreate carCreate = new CarCreate();
|
||||
carCreate.setName("create car");
|
||||
carCreate.setOperatorUserId("create user id....");
|
||||
|
||||
CarDTO car1 = converter.convert(carCreate, CarDTO.class);
|
||||
log.info("create user : {}", car1);
|
||||
Assert.isTrue(car1.getName().equals("create car"), "create user name is empty");
|
||||
Assert.isTrue(car1.getCreateBy().equals("create user id...."), "create user id is empty");
|
||||
|
||||
CarCreate carCreate1 = converter.convert(car1, CarCreate.class);
|
||||
Assert.isTrue(carCreate1.getName().equals("create car"), "create user name is empty");
|
||||
Assert.isTrue(carCreate1.getOperatorUserId().equals("create user id...."), "create user id is empty");
|
||||
|
||||
|
||||
CarUpdate carUpdate = new CarUpdate();
|
||||
carUpdate.setName("update car");
|
||||
carUpdate.setOperatorUserId("update user id....");
|
||||
|
||||
CarDTO car2 = converter.convert(carUpdate, CarDTO.class);
|
||||
log.info("update user : {}", car2);
|
||||
Assert.isTrue(car2.getName().equals("update car"), "update user name is empty");
|
||||
Assert.isTrue(car2.getUpdateBy().equals("update user id...."), "update user id is empty");
|
||||
|
||||
CarUpdate updateCar1 = converter.convert(car2, CarUpdate.class);
|
||||
Assert.isTrue(updateCar1.getName().equals("update car"), "update user name is empty");
|
||||
Assert.isTrue(updateCar1.getOperatorUserId().equals("update user id...."), "update user id is empty");
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -43,6 +43,6 @@ public class SpringComponentProcessor extends AnnotationBasedComponentModelProce
|
||||
InjectionStrategyGem injectionStrategy) {
|
||||
return new SpringDelayInjectMapperReference(originalReference.getType(), originalReference.getVariableName(),
|
||||
originalReference.isUsed(),
|
||||
getTypeFactory().getType("io.github.linpeilie.mapstruct.SpringContextUtils"));
|
||||
getTypeFactory().getType("io.github.linpeilie.mapstruct.SpringContextUtils4Msp"));
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,2 +1,2 @@
|
||||
<#-- @ftlvariable name="" type="io.github.linpeilie.processor.enhance.model.SpringDelayInjectMapperReference" -->
|
||||
private <@includeModel object=type/> ${variableName} = SpringContextUtils.getBean("${variableName}", <@includeModel object=type/>.class);
|
||||
private <@includeModel object=type/> ${variableName} = SpringContextUtils4Msp.getBean("${variableName}", <@includeModel object=type/>.class);
|
||||
@ -25,8 +25,8 @@ public class MapstructAutoConfiguration {
|
||||
}
|
||||
|
||||
@Bean
|
||||
public static SpringContextUtils springContextUtils() {
|
||||
return new SpringContextUtils();
|
||||
public static SpringContextUtils4Msp springContextUtils() {
|
||||
return new SpringContextUtils4Msp();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -9,20 +9,20 @@ import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
|
||||
public class SpringContextUtils implements BeanFactoryPostProcessor, ApplicationContextAware {
|
||||
public class SpringContextUtils4Msp implements BeanFactoryPostProcessor, ApplicationContextAware {
|
||||
|
||||
private static ConfigurableListableBeanFactory beanFactory;
|
||||
private static ApplicationContext applicationContext;
|
||||
|
||||
public SpringContextUtils() {
|
||||
public SpringContextUtils4Msp() {
|
||||
}
|
||||
|
||||
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
|
||||
SpringContextUtils.beanFactory = beanFactory;
|
||||
SpringContextUtils4Msp.beanFactory = beanFactory;
|
||||
}
|
||||
|
||||
public void setApplicationContext(ApplicationContext applicationContext) {
|
||||
SpringContextUtils.applicationContext = applicationContext;
|
||||
SpringContextUtils4Msp.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
public static ApplicationContext getApplicationContext() {
|
||||
Loading…
x
Reference in New Issue
Block a user