Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

When using IDEA to develop projects, it is common to see that there is a red wavy line under some injected properties. When you put the mouse on it and check the problem, it will prompt “Could not autowire”. Although it may not affect the operation of the program, sometimes you want to remove them if you are compulsive. This time we will analyze the Could not Autowire problem generated by injection.

1. Problem description

When using SpringBoot+MyBatis framework to develop projects, there are often dependency injection errors in the Service layer, as follows:

If we place the mouse over the error position, it will prompt:

Could not autowire. No beans of ‘ProductCodeMapper’ type found.

2. Cause of the problem

The cause of the problem is that the corresponding injection field is not found, if not the corresponding class information, it is the compatibility of the development framework.

When this problem occurs in the Service layer referencing Mapper files, it is because the @Mapper annotation used by Mapper files is provided by MyBatis and the auto-injected @AutoWired annotation is provided by Spring.

IDEA can easily understand the Spring context, but it is not well correlated with MyBatis framework. Therefore, although the object marked by @mapper annotation in MyBatis exists, Spring cannot discover it in IDEA.

Also, Spring’s @AutoWired annotation by default requires the injected object to exist. IDEA will report an error if the object is not detected. Set to false to disable detection.

@Target({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Autowired {
    /**
     * Declares whether the annotated dependency is required.
     * <p>Defaults to {@code true}.
     */
    boolean required(a) default true;
}
Copy the code

3. Problem solving

In fact, this problem is only red warning given by IDEA, which does not affect the normal execution of the program. But if it looks uncomfortable and we want to solve it, we can start from the cause of the problem and try to solve it.

  1. Since it is the red warning of IDEA, we can turn off the warning of IDEA or lower the warning level, so that the warning will not appear
  2. Spring’s @AutoWired annotation requires that the injected object must exist by default. We can manually set it to false, i.e. IDEA will no longer alarm if the injected object exists
  3. Replacing the @Autowired annotation with the @Resource annotation also stops errors because the @Resource annotation is provided by J2ee and the @Autowired annotation is provided by Spring.
  4. Append the @Component annotation to the Mapper interface to indicate that the Mapper is being injected into the Spring container, so IDEA can associate them without error
  5. Change to a different injection method, such as using constructor injection, which is recommended in spring 4.x
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
//@AllArgsConstructor
public class ChargeServiceImpl implements ChargeService {

    private final RedisTemplate<String, Object> redisTemplate;
    private final CarUserMapper carUserMapper;
    private final ChargingMapper chargingMapper;
}
Copy the code