Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem that there are errors when using @ Autowired annotations

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "how to solve the problem of using @ Autowired annotations with error tips". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to solve the problem of using @ Autowired annotations."

There is an error prompt with @ Autowired annotation

When using the Spring boot + mybatis framework, you use the Mapper class in the service implementation class, and when you add @ Autowired annotations to the Mapper class, you find

Error message: could not autowire,no beans of "XXX" type found

But the program compiles and runs normally.

Treatment mode

Scenario 1: @ Autowired (required = false) sets the required attribute to false, and the error disappears

Option 2: replace @ Resource annotation with @ Autowired annotation, the error disappears

Similarities and differences between @ Resource annotation and @ Autowired annotation

Both annotations are used for bean injection, omitting to write the get,set method for an object variable, and there are differences in how to automatically inject instantiated objects (that is, injection dependencies) into this object:

@ Autowired is based on the spring annotation org.springframework.beans.factory.annotation.Autowired, which is assembled by type by default. If you want it to be assembled by name, you need to add @ qualifier ("name") `annotation under @ autowired. An error is reported when the only implementation class cannot be found. The @ Autowired annotation must require that dependent objects exist by default. If you want to allow null values, you should set its required property to false

@ Resource is based on j2ee annotations (which can reduce the coupling with spring). By default, JDK1.6 supports annotations by name. If you do not specify the name of the assembly bean, when the annotation is written on the field, the field name is taken by default, and the name is found to be assembled through the set method. If there are multiple subclasses, an error will be reported. It is important to note that once the name attribute is specified, it will only be assembled by name

Spring's @ Autowired comment changed error report to warning

Using the @ Autowired annotation, there will be an error prompt in idea, which does not affect the normal operation of the program. As far as I can see, most of my colleagues choose to ignore this error, but it is really hard to put a red line here. I later learned that you can change the error here to warning without interfering with other code.

Open the settings of idea, find the location shown in the following figure, and change the error error of automatic injection to warning warning.

After successful modification, there will be no annoying red underlining (turned yellow), and it is not recommended to modify these tests at will unless you really know it.

At this point, I believe you have a better understanding of "how to solve the problem of using @ Autowired annotations". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report