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 @ autowired can not be used for object bean injection in new

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "how to solve the problem that @ autowired can not be used for object bean injection in new". In the daily operation, I believe that many people can not use @ autowired for object bean injection in new. There are doubts about how to solve the problem. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubt that "you can't use @ autowired for object bean injection in new". Next, please follow the editor to study!

The object from new cannot be injected into object bean using @ autowired

The @ autowired injection was used in my new class, but the test found that it was not injected and the object was empty

For example, the User entity needs to call the mapper of another entity to query

The @ PostConstruct annotation is used here to solve the problem:

Create a new static object and initialize it with @ PostConstruct. Because I am using this class, I use private. If you want to use an external class, you can change it to public.

New creates objects, and member objects use @ autowired injection to null

An object created using new, in which the member object injected by @ Autowired is null.

So if there is an object injected by the spring container in a class, you cannot use new to create the object, you must use @ Autowired, @ Resource to create the object.

This means that the object is also managed by the spring container.

At this point, the study on "how to solve the problem that @ autowired can not be used for object bean injection in new" 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.

Share To

Development

Wechat

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

12
Report