In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to solve the variable red under @ Autowired annotation in idea". In daily operation, I believe many people have doubts about how to solve the problem of variable red under @ Autowired annotation in idea. Xiaobian consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer "how to solve the problem of variable red under @ Autowired annotation in idea". Next, please follow the editor to study!
Variable dividend problem under @ Autowired annotation in idea
Variable red under @ Autowired annotation in idea
This is not a mistake, it's just a warning. In fact, you can start a project without solving it.
Solution method
1. Modify settings
File- > settings- > Inspections- > spring Core- > Code- > Autowiring for Bean Class
2. Change the note @ Autowired to @ Resource
3. Note the attribute required = false of @ Autowired
4. Add the annotation @ Repository ("UserDao") to the corresponding dao layer code
Note @ Autowired-- warning (pro test is available) @ AutowiredUserDao userDao
The warning is
Field injection is not recommended
It means that using variable dependency injection is not recommended.
The solution strategy for using idea is as follows:
Always use constructor based dependency injection in your beans. Always use assertions for mandatory dependencies
It means that you always use a constructor to force injection.
There are three ways of dependency injection:
Variable (filed) injection
Constructor injection
Set method injection
Let's take a look at the implementation first.
Variable (filed) injection @ AutowiredUserDao userDao; Constructor injection finalUserDao userDao;@Autowiredpublic UserServiceImpl (UserDao userDao) {this.userDao = userDao;} set method injection private UserDao userDao;@Autowiredpublic void setUserDao (UserDao userDao) {this.userDao = userDao;}
(I used the third method when I encountered this error.)
By comparison:
Advantages: variable injection is very simple, without any extra code, which effectively improves the simplicity of java. Even a few more dependencies can solve the problem.
Disadvantages: unable to effectively identify dependencies. I believe many people have come across a bug, and the object of dependency injection is null. The problem encountered when starting the dependency container is that the configured dependency injection is missing an annotation or something. However, this way is too dependent on the injection container. When the entire dependency container is not started, the class will not work and cannot provide the dependency needed by this class during reflection.
When using set, this is an optional injection, which is optional, and even if the dependency is not injected, it does not affect the operation of the entire class.
When using the constructor mode, it has been explicitly stated that the injection must be forced. This class is guaranteed to run by forcing dependency injection.
On the other hand:
One of the core ideas of dependency injection is that container-managed classes should not rely on container-managed dependencies. In vernacular, if this class uses dependency injection classes, then this class must work properly without these dependencies. However, the use of variable injection does not guarantee this.
Now that dependency injection is used, it shows that the class is no longer responsible for these dependencies, which are managed by the container, so how do you know exactly what dependencies the class needs? It uses either set method injection or constructor injection.
At this point, the study on "how to solve the variable red under the @ Autowired annotation in idea" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.