2017-06-16 7 views
0

私は、Springフレームワークをベースにしたアプリケーションを持っています。いくつかの要件を実装するために私はApplicationContextの実装を提供しなければなりませんでした。カスタムApplicationContext実装のスプリングブートアプリケーション

spring-bootに移動しました。起動するように強制する方法があれば、私はApplicationContextの実装を使用していますか?

EDIT:提供コンテキストが最後から二番目の行で作成されたいくつかのコード

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実装です。そして、これは、スプリング・ブートがなくても、スプリング・アプリケーションが作成される方法です。

+0

'org.springframework.boot.SpringApplication.applicationContextClass'のパブリックセッターがあります。ですから、静的な 'run(..)'メソッドを使っている間にそれを提供する方法があると思います。 – phani

+0

@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

+0

私はこれをあなたのような最初のタイマーにしたことはありません。方法については、http://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/builder/SpringApplicationBuilder.html#contextClass-java.lang.Class- – phani

答えて

-1

実装が可能で、リフレッシュ、開始などの多くのリスナーを必要とするインタフェースApplicationListenerがあります。

public class ApplicationListenerBean implements ApplicationListener<ContextRefreshedEvent> { 

@Override 
public void onApplicationEvent(ContextRefreshedEvent event) { 
    ApplicationContext applicationContext = event.getApplicationContext(); 
    // now you can do applicationContext.getBean(...) 
    // ... 
} 

}

これが役立つことを願っています。

+0

'ApplicationContext'の型が間違っています。 – hotzst

関連する問題