questions

Could not autowire (IDE = IntelliJ IDEA)

Although the red line indicates the error, there is no problem in operation and use. Looking at the red line error uncomfortable, the search got several schemes, integrated together.

Problem solving

1. Replace @autowired with @resource

As follows:

What is the difference between @autowired and @Resource? – wuxinliulei answer – zhihu www.zhihu.com/question/39… :

  1. Both @autowired and @Resource can be used to assemble beans. You can write it on a field, or you can write it on a setter method.
  2. By default, @autowired must assemble by type (this annotation is owned by Spring). By default, the dependent object must exist. To allow null values, you can set its required property to false, such as @Autowired(Required =false).
  3. @Resource is a JDK1.6 supported annotation that is assembled by name by default. The name can be specified by the name attribute. If the name attribute is not specified, when annotations are written to a field, the default is the field name, which is searched by name. If annotations are written on setter methods, they are assembled by type if no bean matching the name is found. Note, however, that if the name attribute is specified, it will only be assembled by name.

2. Set the attribute value for the annotation, @autowired (Required =false)

As follows:

By default, @autoWired assembs by type (this annotation belongs to Spring). By default, you must require the dependent object to exist. If you want to allow null values, you can set its required property to false

3. Add a annotation to mapper @Component(value = “XXX “)

(Citing this article blog.csdn.net/qq_21853607…)

Insert mapper member variable name as follows:

4. (Wrong plan!) Lombok, you can just use @allargsconstructor

As follows:

A quote from @Programmers who Like listening to music (me.csdn.net/zz153417230)

If you use lombok tools, you can generate an all-parameter constructor directly with the @allargsconstructor annotation. Starting with Spring4.3, if there is only one constructor, it is automatically assembled.

ParameterResolver registered for parameter No ParameterResolver registered for parameter could not autowire

The reason:

Lombok uses @allargsconstructor to generate an all-parameter constructor. First, my Mapper is an interface, and interfaces have no constructors. In addition, injection is required here, and the container does the instantiation of the object rather than generating the constructor, hence the error.

An example of a constructor that @allargsconstructor can automatically generate is as follows:

The problem summary

Personally, I prefer the first approach, which is simpler: use @resource instead of @AutoWired