Spring Boot:从外部Jar文件引入配置类

huangapple go评论85阅读模式
英文:

Spring Boot: Bring In Configuration Classes From External Jar File

问题

我正在尝试减少我在多个Spring项目中拥有的“复制/粘贴”代码数量,例如自定义的WebSecurityConfigurerAdapter。这个适配器在我迄今为止的所有项目中都是完全相同的代码,所以通常在这种情况下,我会创建一个独立的代码库并将其导出为一个JAR文件,以保持代码的可维护性。

问题是,当我这样做时,似乎Spring不再注意到这个类,不再将其视为需要纳入自动配置的类。因为当我从项目中移除了这个安全类并引入包含它的JAR库时,我开发的安全功能就无法正常工作了。撤销了这个更改后,它又恢复正常了。

有没有办法告诉Spring查看特定的JAR文件,以将其纳入到配置中,这样我就可以避免在多个项目中重复编写大量配置代码呢?

英文:

I'm trying to reduce the amount of "copy/paste" code I have across multiple Spring projects, such as a custom WebSecurityConfigurerAdapter. This adapter is the exact same code across all projects I have so far, so normally in these situations I create a standalone code base and export to a jar to keep things maintainable.

The issue is that it seems that when I do that, Spring no longer notices that class as one that it needs to ingest into it's auto configuration, as when I removed that security class from my project and brought in the jar library now containing it, the security feature I developed was broken. Undid that change and it was working again.

Is there any way that I can tell Spring to look at specific jar files to ingest into it's configuration so that I can keep myself from having classes upon classes of configuration code that's ultimately repeated across multiple projects?

答案1

得分: 1

你可以在你的应用程序类中使用带参数的 @ComponentScan 注解来指向多个模块。详细说明可以在 这里 找到。这将覆盖在 @SpringBootApplication 中包含的默认模块扫描。

英文:

You can use the @ComponentScan annotation with arguments in your application class to point to multiple modules. It is explained here. This will override the default one included in @SpringBootApplication

huangapple
  • 本文由 发表于 2020年8月6日 05:10:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/63273563.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定