“This article has participated in the call for good writing activities, click to view: the back end, the big front end double track submission, 20,000 yuan prize pool waiting for you to challenge!”

The error message above is something most programmers have encountered.Strange is that although the code error, but does not affect the normal execution of the programThe compiler IDEA reported an error, but the program can be executed normally.

Error cause analysis

First of all, the error is due to the powerful alarm mechanism of IDEA. @Autowired is the annotation of Spring, which means to dynamically inject a certain class into the current class, as shown in the following figure:By default, @autowired is injected based on type and requires that the object (injected) cannot be NULL, as shown below:IDEA reported the error because: @autowired is the annotation of Spring, and the injected Mapper object uses the annotation of @Mapper. However, @mapper is the annotation of MyBaits. IDEA is compatible with Spring annotations. However, MyBatis annotations are not well recognized, so when using @AutoWired annotations, IDEA does not detect that the @mapper annotation object is not NULL, so an error will be reported.

That’s why the Spring annotation @repository / @component… The root cause of an error that does not occur when using the @mapper annotation is shown below:

Solution 1: Turn off the alarm mechanism

To avoid errors, disable the IDEA injection alarm mechanism. The implementation procedure is as follows.

1. Open IDEA and find the Preferences… , as shown in the figure below:2. Choose “Editor” -> “Conforms” -> “Spring” -> “Spring Core” -> “Code” -> “Autowiring for Bean Class” to change the “Error” level to “Waring” level, as shown below:After setting, click OK to view the Mapper class that reported an error. The display effect is as follows:The error message disappeared.

Solution 2: Add Spring annotations

The IDEA error can also be resolved by adding Spring annotations to Mapper classes, such as @repository or @Component, as shown below:Or use the @repository annotation, as shown below:Check the previous error message:The error disappeared.

Solution 3: Allow injection objects to be NULL

To avoid IDEA errors, set @AutoWired (Required =false) as shown in the following figure:(The userMapper2 object does not report errors.)

  • @autowired (required=true) : Indicates that the bean must exist when the @autowired annotation is used, otherwise injection will fail, default.
  • @autoWired (Required =false) : Ignores the bean to be injected. If there is direct injection, no error will be reported.

Autowired implements the default value @autowired.

Solution 4: Use the @Resource annotation

Using the @Resource annotation instead of the @Autowired annotation can also avoid errors, and their comparison looks like this:The main difference between the @Resource annotation and the @AutoWired annotation in the current scenario is that @Resource is a Java native annotation, while @AutoWired is a Spring native annotation, @AutoWired defaults to required=true, so a non-NULL object is required. IDEA will report an error if the object is not NULL. @Resource does not require this.

conclusion

Import Mapper object with @AutoWired annotation because @Autowired needs to inject a non-null object by default, and the class modified by @Mapper is MyBatis annotation. IDEA does not recognize it as a non-null object very well, so an error is reported. Of course, there are many solutions, and it is recommended to use @Resource instead of @AutoWired annotations to solve this problem.

More Spring dry goods, follow the public account “Java Chinese community”, real-time access.