Day 3 Part 1: Dish Management (AOP, Custom Annotations, Reflection, File Upload)

The automatic filling of common fields primarily uses AOP (Aspect-Oriented Programming), involving reflection mechanisms and custom annotations. For adding new dishes, we learn about file upload techniques using Alibaba Cloud OSS for cloud storage, along with handling relationships between two tables—retrieving dish IDs from the dish_flavor table related to the dish table.

Common Field Population (Enums, Custom Annotations, AOP, Reflection)

Problem Analysis

Duplicating code for setting common fields in business entities leads to maintenance issues. Here's an example:

// Setting creation time, update time, creator, and updater for employee
employee.setCreateTime(LocalDateTime.now());
employee.setUpdateTime(LocalDateTime.now());
employee.setCreateUser(BaseContext.getCurrentId());
employee.setUpdateUser(BaseContext.getCurrentId());

// Setting same fields for category
category.setCreateTime(LocalDateTime.now());
category.setUpdateTime(LocalDateTime.now());
category.setCreateUser(BaseContext.getCurrentId());
category.setUpdateUser(BaseContext.getCurrentId());

Implementation Approach

  1. Custom Annotation AutoFill: Used to mark methods requiring automatic population of common fields.
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface AutoFill {
    OperationType value();
}

  1. Aspect Class AutoFillAspect: Intercepts methods annotated with AutoFill using AOP, then applies reflection to populate common fields.
@Aspect
@Component
@Slf4j
public class AutoFillAspect {

    @Pointcut("execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.annotation.AutoFill)")
    public void autoFillPointCut() {}

    @Before("autoFillPointCut()")
    public void autoFill(JoinPoint joinPoint) {
        log.info("Starting automatic field population...");
        
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        AutoFill autoFill = signature.getMethod().getAnnotation(AutoFill.class);
        OperationType operationType = autoFill.value();
        Object[] args = joinPoint.getArgs();
        
        // Process entity object based on operation type
        if (args.length > 0) {
            Object entity = args[0];
            try {
                // Use reflection to set common fields
                Method setCreateTime = entity.getClass().getMethod("setCreateTime", LocalDateTime.class);
                Method setUpdateTime = entity.getClass().getMethod("setUpdateTime", LocalDateTime.class);
                Method setCreateUser = entity.getClass().getMethod("setCreateUser", Long.class);
                Method setUpdateUser = entity.getClass().getMethod("setUpdateUser", Long.class);

                LocalDateTime now = LocalDateTime.now();
                Long userId = BaseContext.getCurrentId();

                if (operationType == OperationType.INSERT) {
                    setCreateTime.invoke(entity, now);
                    setCreateUser.invoke(entity, userId);
                }

                setUpdateTime.invoke(entity, now);
                setUpdateUser.invoke(entity, userId);
            } catch (Exception e) {
                log.error("Error during auto-fill: " + e.getMessage());
            }
        }
    }
}

Tags: aop custom annotation reflection File Upload Alibaba Cloud OSS

Posted on Sun, 09 Aug 2026 16:04:58 +0000 by tzuriel