Java 具有自定义ApplicationContext实现的Spring引导应用程序
我有一个基于Spring框架的应用程序。为了实现一些需求,我必须提供自己的Java 具有自定义ApplicationContext实现的Spring引导应用程序,java,spring,spring-boot,Java,Spring,Spring Boot,我有一个基于Spring框架的应用程序。为了实现一些需求,我必须提供自己的ApplicationContext实现 现在我转到了spring boot,我想知道是否有任何方法可以强制启动以使用我的ApplicationContext实现 编辑:提供一些代码 public class OpenPatricianSpringBootStandaloneApplication implements CommandLineRunner { private static Logger logger
ApplicationContext
实现
现在我转到了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();
}
dependentNotationConfigApplicationContext
是我正在使用的ApplicationContext
实现。这就是在没有spring引导的情况下创建spring应用程序的方式。有一个接口ApplicationListener,可以实现,它需要许多侦听器,比如Refresh、Start
public class ApplicationListenerBean implements ApplicationListener<ContextRefreshedEvent> {
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
ApplicationContext applicationContext = event.getApplicationContext();
// now you can do applicationContext.getBean(...)
// ...
}
公共类ApplicationListenerBean实现ApplicationListener{
@凌驾
ApplicationEvent(ContextRefreshedEvent事件)上的公共无效{
ApplicationContext ApplicationContext=event.getApplicationContext();
//现在您可以执行applicationContext.getBean(…)
// ...
}
}
希望这会有所帮助。org.springframework.boot.SpringApplication.applicationContextClass有一个公共setter。因此,我想在使用static
run(…)
方法时,可能有一种方法可以提供这种功能。@phani:看一下,我没有看到任何方法来设置应用程序上下文类。但是,可能会使用。你愿意举个例子吗?我从来没有这样做过,像你这样的第一次。这样做会怎么样?因为ApplicationContext
的类型错误。