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 Spring boot cannot inject service

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

Share

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

This article focuses on "how to solve the problem that Spring boot cannot be injected into service". Interested friends may wish to have a look. 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 that Spring boot cannot be injected into service.

Catalogue

Spring boot cannot inject service

@ Autowired injection cannot instantiate service

Problem scenario

Problem interpretation

Solution

@ PostConstruct description

Spring boot cannot inject service

The default rule for Bean assembly for SpringBoot projects is to scan from top to bottom based on the package location where the Application class is located!

"Application class" refers to the SpringBoot project entry class.

The location of this class is critical:

If the package of the Application class is: com.boot.app, only the com.boot.app package and all its subpackages will be scanned, and if the package of service or dao is not under com.boot.app and its subpackages, it will not be scanned!

That is, put the Application class in the parent of the package where dao and service are located, com.boot.Application

It is critical to know that this is the reason why bean cannot be injected in most cases.

@ Autowired injection cannot instantiate a service problem scenario

When springboot integrates netty, the processing class of netty cannot instantiate service

Problem interpretation

After consulting the data, the bean object of netty is not managed by spring by default.

Solution @ Component//1. Add the annotation public class NettyServerHandler extends ChannelInboundHandlerAdapter {private static Log log = LogFactory.getLog (NettyServerHandler.class); @ Autowired private RedisUtils redisUtils; / / 2. Declare this class private static NettyServerHandler nettyServerHandler; / / 3. Declare the construction scheme public NettyServerHandler () {} / / 4. Add comments and execute this method @ PostConstruct public void init () {nettyServerHandler = this; nettyServerHandler.redisUtils = this.redisUtils;} = = after the above four steps of processing for (Map dto: variants) {String key = dto.get ("key"). ToString (); nettyServerHandler.redisUtils.hmset (dto.get ("key"). ToString (), dto, 100000) } / / nettyServerHandler.redisUtils.hmset () can call redisUtils in this way to describe @ PostConstruct

@ PostConstruct

Starting with the Java EE5 specification, Servlet has added two annotations that affect the Servlet lifecycle (Annotation): @ PostConstruct and @ PreConstruct. These two annotations are used to modify a non-static void () method. And this method cannot have an exception declaration. @ PostContruct is the annotation of the spring framework, and adding this annotation to the method executes the method when the project starts, or it can be understood to execute the method when the spring container is initialized.

The use of @ PostConstruct in the project

The @ PostConstruct annotated method executes this method when the project starts, or it can be understood to be executed when the spring container starts, which loads regular data in the project.

The order of Constructor, @ Autowired, @ PostConstruct in spring

To inject object b into object a, you must first generate object b and object a before you can perform the injection. So, if a member variable b in a class an is annotated with @ Autowired, the @ Autowired injection occurs after the constructor of an is executed.

If you want to complete some initialization operations when the object is generated, but these initialization operations depend on dependency injection, you cannot do so in the constructor. To do this, you can use @ PostConstruct to annotate a method to complete initialization, and the method annotated by @ PostConstruct will be called automatically after dependency injection is complete.

Constructor > > @ Autowired > > @ PostConstruct

At this point, I believe you have a deeper understanding of "how to solve the problem that Spring boot cannot be injected into service". 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