Intellij IDEA in Mybatis Mapper automatically inject six kinds of solutions warnings

Point of attention, do not get lost; continually updated Java-related technologies and information architecture thermal paper! ! !

I believe the use of Mybaits little friends will often write code like the following:

You can see a red warning under userMapper. Although the code itself is not a problem, normal operation, but there is a warning it is always a bit sick. This paper analyzes the reasons and lists several options to address the warning.

the reason

As we all know, IDEA is very intelligent, it can be understood in the context of the Spring. However UserMapper of this interface is Mybatis, IDEA can not understand.

And @Autowired notes, by default requirements dependent objects (ie userMapper) must be present. The IDEA think instance of this object / proxy is null, so we kind enough to give a hint.

solution

Method 1: Set @Autowired annotation is required = false

When using @Autowired annotation, if desired null value is permitted, it may be provided required = false, like this:

@Autowired(required = false)
private UserMapper userMapper;

In this way there would be no warning. The reason is well understood: IDEA think userMapper is null, gave warning; after adding required = false, use @Autowired notes no longer go check whether there is a userMapper. There would be no warning.

Summary: In this way a little bit boring. Both a huge project, probably everywhere in reference Mapper, everywhere can not make up required = false it ...... but for the novice / new employees, it is difficult to understand a plus required = false attribute only to solve IDEA warning.

Method 2: Alternatively @Autowired with @Resource

like this:

@Resource
private UserMapper userMapper;

This also will be no more annoying warning. If you are interested in the cause, check out "@Autowired @Resource the difference between" [1]

Summary: In this way quite like this, but if a project has been widely used @Autowired, then changed to a warning @Resource everywhere, a bit boring.

Method 3: Notes on the Mapper plus @Repository Interface

like this:

@Repository
public interface UserMapper extends Mapper<User> {
}

This also allows you

@Autowired
private UserMapper userMapper;

No error.

Of course, if you replace @Repository with @Component also possible. Principle roughly: IDEA not think userMapper is null Well ... @Repository add a comment about the IDEA cheat on OK ......

Summary: It's more like, change small, simple, I like it better.

Method 4: Lombok

like this:

@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class TestService {
    private final UserMapper userMapper;
    ...
}

Lombok generated code is such that:

@Service
public class TestService {
    private final UserMapper userMapper;
    @Autowired
     public TestService(final UserMapper userMapper) {
        this.userMapper = userMapper;
    }
    ...
}

But if your own handwriting to Lombok generated code, IDEA reported to still give you a warning. I guess, I should be put IDEA IDEA plug-in Lombok confused by the force ... so do not prompt a ...

Summary: This is my current favorite way. There are two reasons:

  • Spring official not recommended @Autowired annotation directly on the class field, see Reason: "Why field injection is evil" [2], using the present process may be injection programming field Constructor injection, Spring is more recommended.
  • It embodies the advantages of Lombok, simplifying your code. And you do not have to add in each field are @Autowired annotated.

However, this approach has its drawbacks: it is when the dependencies between classes if you compare complex, especially a circular dependency (A reference to B, B references A, or indirect reference), references will not start up ...... This is actually constructor disadvantage is injection method.

Method 5: IDEA warning to shut off

Individuals have not tried, and no incentive to try. Without prompting IDEA is no soul, and I never want to change any settings IDEA's warning.

Method 6: Installation mybatis plugin

It is said that the installation mybatis plugin can solve the problem. But this plug-in is a fee, I do not have the money to try. Wealthy readers could tell me is not true.

to sum up

These are 6 ways to solve the problem. In fact, the problem itself is relatively simple, but in fact the hidden point of fact, a lot of knowledge, such as:

  • What is the difference @Autowired and @Resource
  • Why Spring is not recommended mode field injection
  • @ Repository, @ Componnt, @ Controller, @ Service What is the difference

In short, it is sometimes hard goods hidden under the question is very low, ha ha ha.

Guess you like

Origin www.cnblogs.com/Java-no-1/p/11025694.html