简体   繁体   中英

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:

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

How to load beans dynamically? 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?

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.

Do you know any other solutions or what is wrong? Will this work for spring rest controllers from 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. 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:

  1. Define your own @Component annotation:
// runtime retention, place on class
@Component
@ConditionalOnProperty(...)
@MyFeatureComponent

... and use it in all the beans that define the feature:

@MyFeatureComponent
public class MyBean
{}
  1. Use Java Configuration instead of annotations:
@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.

Spring also has a concept of profiles which is just the same, something that it under the hood implemented with these conditionals. It allows however to define configuration files per profile, so you might want to read about @Profile annotation as well.

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. 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. 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. I doubt, but if you really need that, read about Bean Factory Post Processors in spring.

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