79640784

Date: 2025-05-27 15:35:59
Score: 0.5
Natty:
Report link

When you declare:

@Mapper(
        uses = {StudentMapper.class}
)
public interface ProgramMapper {
@Mapper(
        uses = {ProgramMapper.class}
)
public interface StudentMapper {

You're causing the cyclic reference because Spring gets stuck in a loop trying to resolve both beans at creation time. Spring can't resolve this for interfaces because it creates proxy implementations at runtime and has no way to inject one mapper into another after creation.

You can remove it and try to make it an abstract class i.e.

@Mapper(componentModel = "spring", uses = {})
public abstract class ProgramMapper {

    @Autowired @Lazy
    protected StudentMapper studentMapper;

    public abstract Program toProgram(ProgramDTO dto, @Context CycleAvoidingMappingContext context);

    public abstract ProgramDTO toProgramDTO(Program entity, @Context CycleAvoidingMappingContext context);
}

Alternatively Use interfaces + a mapping service class to handle dependencies explicitly.

side note: removing the @builder when using mapstruct is cleaner — just use either one

Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @builder
  • Starts with a question (0.5): When you
  • Low reputation (0.5):
Posted by: Rickky13