简体   繁体   中英

load multiple springboot app with annotation in springboot

I am having a springboot application to load different classes dynamically based on the user input.

Can someone suggest any better and optimised way for doing this.

Below is my approach:-

//I have a map of actions available with their respective classes(which is also a springboot app)
private static Map<String,Class> actions = ImmutableMap.<String,Class>builder()
                                                       .put("A",A.class),
                                                       .put("B",B.class);

//main method of springboot root app
public static void main(String args[]){
 new MainApplication().run(args);
}

//get the action class and run that application
public void run(String args){
 Class action = getAction();
 SpringApplication app = new SpringApplicationBuilder(job).build();
 app.run(args);
}

//action.name is passed as an argument while starting the application( for.ex action.name="A")
private Class getAction(){
 String action = System.getProperty("action.name");
 Class classType = actionMap.get(action);
 return classType;
}

Now I want to load these action applications with just an annotation. Can someone suggest any way to do the same.

Use conditional Bean concept as below

@Service
@ConditionalOnProperty(
        value="action.name", 
        havingValue = "a", 
        matchIfMissing = false)
public class TestBeanConditional {

}

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