如何从单独的 Jars 加载 @Configuration 类 [英] How to load @Configuration classes from separate Jars
问题描述
我有一个 SpringBoot 主应用程序,以及一个单独的 Maven 模块项目,它编译为单独的 Jar.该模块有一个用 @Configuration
注释的 Spring 配置类,我想在主应用程序加载时加载它.
I have a SpringBoot main application, as well as a separate Maven module project that compiles as a separate Jar. The module has a Spring config class annotated with @Configuration
, which I want to get loaded, when the main app loads.
显然,这不是开箱即用的(只需将模块包含到主应用程序中).我还需要做什么才能让模块配置类也被主应用程序加载?
Apparently, this does not happen out of the box (by just including the module to the main app). What else do I need to do, to get the module configuration class also get loaded by the main app?
推荐答案
最简单的方法就是扫描@Configuration类所在的包.
The easiest way is to scan the package that the @Configuration class is in.
@ComponentScan("com.acme.otherJar.config")
或者只是将其作为 spring bean 加载:
or to just load it as a spring bean:
@Bean
public MyConfig myConfig() {
MyConfig myConfig = new MyConfig ();
return myConfig;
}
其中 MyConfig 类似于:
Where MyConfig is something like:
@Configuration
public class MyConfig {
// various @Bean definitions ...
}
见文档
这篇关于如何从单独的 Jars 加载 @Configuration 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!