我有一個基於Spring框架的應用程序。爲了實現一些要求,我必須提供我自己的ApplicationContext
的實現。具有自定義ApplicationContext實現的Spring引導應用程序
現在我轉移到spring-boot
我想知道是否有任何方法來強制啓動使用我的ApplicationContext
執行?
編輯:提供一些代碼
public class OpenPatricianSpringBootStandaloneApplication implements CommandLineRunner {
private static Logger logger = LogManager.getLogger(OpenPatricianSpringBootStandaloneApplication.class);
public static void main(String[] args) throws Exception {
SpringApplication.run(OpenPatricianSpringBootStandaloneApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
String jreVersion = (String) System.getProperties().get("java.version");
if (!jreVersion.startsWith("1.8")) {
logger.error("JRE must be of version 1.8");
System.out.println("JRE must be of version 1.8");
System.exit(1);
}
logEnvironment();
CommandLineArguments cmdHelper = new CommandLineArguments();
Options opts = cmdHelper.createCommandLineOptions();
CommandLine cmdLine = cmdHelper.parseCommandLine(opts, args);
if (cmdLine.hasOption(CommandLineArguments.HELP_OPTION)){
cmdHelper.printHelp(opts);
System.exit(0);
}
if (cmdLine.hasOption(CommandLineArguments.VERSION_OPTION)) {
System.out.println("OpenPatrician version: "+getClass().getPackage().getImplementationVersion());
System.exit(0);
}
cmdHelper.persistAsPropertyFile(cmdLine);
unpackPlugins();
ServerLauncher.initializeApplicationContext(StandaloneConfiguration.class);
OpenPaticianApplicationWindow.startClientUI(new String[0]);
}
的情況下在第二最後一行創建:
public static void initializeApplicationContext(Class clientServerContextClass) {
Preconditions.checkArgument(baseServerContext == null, "Application baseServerContext is already initialized");
baseServerContext = new DependentAnnotationConfigApplicationContext(clientServerContextClass);
logger.info("Initialize baseServerContext (" + baseServerContext.hashCode() + ") with class "+clientServerContextClass.getName());
((AbstractApplicationContext)baseServerContext).registerShutdownHook();
}
的DependentAnnotationConfigApplicationContext
是ApplicationContext
實現我使用。這就是如何在沒有彈簧啓動的情況下創建彈簧應用程序。
有一個'org.springframework.boot.SpringApplication.applicationContextClass'的公共setter。所以我想在使用靜態的'run(..)'方法時可能會有一種方法來提供這種方法。 – phani
@phani:看看['SpringApplication'](http://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/SpringApplication.html),我沒有看到任何方法來設置應用程序上下文類。但是可能會使用['SpringApplicationBuilder'](http://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/builder/SpringApplicationBuilder.html)。你願意提供一個例子嗎? – hotzst
我從來沒有像你這樣做過第一次。如何http://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/builder/SpringApplicationBuilder.html#contextClass-java.lang.Class- – phani