简体   繁体   English

如何根据Spring java conf从jar动态加载bean?

[英]How to load beans according to Spring java conf from jar dynamically?

I have such app conf in, jar which will be added to the classpath after startup:我有这样的应用程序 conf,jar 将在启动后添加到类路径中:

@Configuration
@ComponentScan("com.transportexchangegroup.testConf")
public class AppConf {
   
}

How to load beans dynamically?如何动态加载bean? I saw solutionsm when it is required to write and add bean definitions, but if we do not know everything about new beans and just want to load them automatically?当需要编写和添加 bean 定义时,我看到了 Solutionsm,但是如果我们对新 bean 一无所知,只想自动加载它们?

Class conf = jarService.loadClass("com.x.testConf.AppConf");
((AnnotationConfigServletWebServerApplicationContext) applicationContext).register(conf);
((AnnotationConfigServletWebServerApplicationContext) applicationContext).refresh();

As I see, refresh() turns off web app started locally from IDE.如我所见,refresh() 关闭了从 IDE 本地启动的 Web 应用程序。

Do you know any other solutions or what is wrong?您知道任何其他解决方案或有什么问题吗? Will this work for spring rest controllers from jar?这对 jar 中的弹簧休息控制器有用吗?

I'm not sure what do you mean "dynamically".我不确定你是什么意思“动态”。 In general you can load beans if some condition applies, usually depending on the configuration.通常,如果某些条件适用,您可以加载 bean,通常取决于配置。 So you can do something like this:所以你可以做这样的事情:

application.properties: // or yaml it doesn't matter

feature.enabled=true
@Component
@ConditionalOnProperty(name="feature.enabled", havingValue="true", matchIfMissing="true" / "false") // matchIfMissing depends on whether you want the bean to be loaded if the property is not defined
public MyBean {

}

Some caveats:一些注意事项:

If you have many beans that depend on "business" feature in order to avoid placing @ConditionalOnProperty you can do one of the following:如果您有许多依赖“业务”功能的 bean 以避免放置@ConditionalOnProperty您可以执行以下操作之一:

  1. Define your own @Component annotation:定义你自己的@Component注解:
// runtime retention, place on class
@Component
@ConditionalOnProperty(...)
@MyFeatureComponent

... and use it in all the beans that define the feature: ...并在定义该功能的所有 bean 中使用它:

@MyFeatureComponent
public class MyBean
{}
  1. Use Java Configuration instead of annotations:使用 Java 配置代替注解:
@Configuration
@ConditionalOnProperty(...)
public class MyFeatureConfiguration {

 @Bean
 public MyBean myBean(){return new MyBean();}

 @Bean
 public MyAnotherBean myAnotherBean(){return new MyAnotherBean();}

}

In this case you don't need to place any annotation on MyBean at all.在这种情况下,您根本不需要在MyBean上放置任何注释。

Spring also has a concept of profiles which is just the same, something that it under the hood implemented with these conditionals. Spring 也有一个 profile 的概念,它是一样的,它在底层实现了这些条件。 It allows however to define configuration files per profile, so you might want to read about @Profile annotation as well.然而,它允许为每个配置文件定义配置文件,因此您可能还想了解@Profile注释。

As for the bean definitions - this is way more advanced stuff, in general when spring loads it "recognizes" which bean should be loaded and in which order and for doing that it creates a bean definition before loading the bean.至于 bean 定义 - 这是更高级的东西,一般来说,当 spring 加载它时,它“识别”应该加载哪个 bean 以及加载顺序,为此它在加载 bean 之前创建一个 bean 定义。 So if you hook into this process you can define your own bean definitions if you want and spring will create beans based on these definitions as well.因此,如果您进入这个过程,您可以根据需要定义自己的 bean 定义,spring 也会基于这些定义创建 bean。 So basically its a hook that allows altering the bean defitions / create new one during the startup process and hence affect the actual beans that will be loaded into the application context.所以基本上它是一个钩子,它允许在启动过程中更改 bean 定义/创建新定义,从而影响将加载到应用程序上下文中的实际 bean。 I doubt, but if you really need that, read about Bean Factory Post Processors in spring.我对此表示怀疑,但如果您真的需要它,请在春季阅读 Bean Factory Post Processors。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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