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/01 Report--
Editor to share with you how to solve the SpringBoot reference other modules can not be injected into the problem, I believe that most people do not understand, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Problems that cannot be injected by referencing other modules
Some time ago, I tried to use my free time to build an IIM message communication system. At present, it is still at the Demo level, and the specific implementation is still being supplemented. I have encountered a small problem in the development process and share it here.
Problem description
Due to the early build in a hurry, put that crazy write call logic, did not pay attention to their own package name is a bit strange.
At present, it is divided into these three modules, the client, the public processing module and the server, but I am thinking of giving this thing the entire routing registry to facilitate the horizontal expansion of the client and the server.
Currently, the package name of my server submodule and the package path of the common submodule are as follows
When I referenced the public class under the common package in the class in the service package, I found that my message dispatcher public class could not be injected.
Emmmmm... I have configured the XML package scan before, but I have not tried the annotation scan.
Solution method
1. Add a parameter to the startup class * * @ SpringBootApplication** to scan the specified package
@ SpringBootApplication (scanBasePackages = {"com.zhl.dispatcher", "server.server"})
It also includes packages for other modules and classes that scan to packages and subpackages. Loaded into the container of Spring.
two。 New comment
@ ComponentScan (value = {"com.zhl.dispatcher", "server.server"})
Second, this annotation is actually a bit redundant. @ SpringBootApplication itself already contains the attribute of scanning the package path, but both methods can be used.
Spring bean of other modules cannot be injected into a multi-module project
A common module and an admin module were created in a springboot project
The common module dependency was introduced into the admin module, and it was found that the bean,idea prompt in the common module could not be found.
After messing around all night, it turns out that there are two module paths that disagree, one is com.example.common, the other is com.example.admin.
This is because the @ SpringBootApplication annotation of springboot scans the package (com.example.admin) and its subpackages where its startup class is located by default, so the module admin does not scan the bean of the module common, so it is naturally impossible to inject the Service class of the module common into the module admin.
Finally:
Method one: just change it to com.example.
Method 2: manually modify it to import multiple packages, @ SpringBootApplication (scanBasePackages = {"com.example.admin", "com.example.common"})
The above is all the content of this article entitled "how to solve the problem that other modules cannot be injected with SpringBoot reference". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.