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 realize no loading of local bean after adding @ ComponentScan in springboot

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Springboot how to add @ ComponentScan after the local bean does not load, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

1. Preface

When springboot needs to load the bean in the jar package, it is usually implemented using the annotation @ ComponentScan (basePackages = {"com.test.http", "com.test.client"}), but @ ComponentScan needs to be careful when using it.

2. Background

Since I need to use springcloud feign to make RPC calls and need to inject api of client, I added @ ComponentScan to scan the packet path where client is located, resulting in an error in the startup times:

Description:

Parameter 0 of method indicatorServiceApi in com.client.ServiceApiConfig required a bean of type 'feign.codec.Decoder' that could not be found.

The antisequence class couldn't find an object to use, so I added a FeignConfig to load the default feign configuration

@ Configuration@Import (FeignClientsConfiguration.class) public class FeignConfig {}

Start again and still report the same error!

3. Investigate the cause

From a phenomenal point of view, client.ServiceApiConfig is at least initialized before FeignConfig, so why not initialize locally first? This is really not.

Look back at the @ ComponentScan comment:

*

Either {@ link # basePackageClasses} or {@ link # basePackages} (or its alias* {@ link # value}) may be specified to define specific packages to scan. If specific* packages are not defined, scanning will occur from the package of the* class that declares this annotation.

This means that if packages is not defined, it starts with the package path of the startup class. This implies that if there is a definition, the package path of the startup class will not be automatically added!

Let's take a look at the above problem, it turns out that the package path of the startup class is different from that of api, causing spring to scan only the package path of api, and of course you can't find the FeignConfig configuration! Just add your own package path to @ ComponentScan.

The packet order of PS:@ComponentScan is not affected.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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

Internet Technology

Wechat

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

12
Report