简体   繁体   中英

How to do component scanning for sibling package in Spring Boot

Currently I have quite a bit of services written in Spring Boot 2.1.3 (in mono-repo) and I have a common package that's used in most of the services.

So my packages are organized in the following:

root
root.common
root.serviceA
root.serviceA.<subpackages>
root.serviceB
root.serviceB.<subpackages>
...
root.serviceX
root.serviceX.<subpackages>

In each service I have Program.java where it's tagged with @SpringBootApplication (eg root/serviceA/Program.java)

The problem now I'm having is that I have a component in root.common (eg root/common/JSONSerializer.java ) which needs to be loaded in all the services. I tried tagging the file with @JsonComponent but obviously it doesn't work because it's outside root.service* package.

I managed to get it working by manually adding @Import tag in the main file but that means that for each service I have to manually add @Import({root.common.JSONSerializer.class}) which is tedious and error prone. Is there a way to include this file in the component scanning process?

You can set each Program class by following way:

 @SpringBootApplication(scanBasePackages = {"root.serviceA", "root.common"})

REFERENCES

SpringBootApplication documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM