英文:
Other ways of kickstarting Spring Boot instead of SpringApplication.run
问题
除了使用SpringApplication.run()
方法之外,运行或启动Spring Boot应用程序还有其他方式吗?例如,AnnotationConfigApplicationContext
能否启动应用程序,为什么?您能列出其他一些方法并加以区分吗?
英文:
I am just curious of there are other ways to run or kickstart a Spring Boot application aside from using SpringApplication.run() method. For example, can AnnotationConfigApplicationContext start the application and why? Can you list some other ways and differentiate it.
答案1
得分: 0
在 Spring Boot 核心模块之前,我们需要处理应用程序上下下文,然后从那里继续,我们必须获取下一个运行的服务类 bean,然后调用该方法。
这里有两种方式,一种是基于 Xml 的配置(XmlBaseConfiguration),另一种是基于注解的配置(AnnotationBaseConfiguration)。
public class XmlBaseConfiguration {
public static void main(String[] args) {
ApplicationContext appContext = new ClassPathXmlApplicationContext(
"META-INF/spring-context.xml");
HelloWorld hw = (HelloWorld) appContext.getBean("helloWorld");
String output = hw.getValueFromContext("strHelloWorld");
System.out.print(output);
}
}
或者
public class AnnotationBaseConfiguration {
public static void main(String[] args) {
ApplicationContext appContext = new AnnotationConfigApplicationContext(Configuration.class);
HelloWorld hw = (HelloWorld) appContext.getBean("helloWorld");
String output = hw.getValueFromContext("strHelloWorld");
System.out.print(output);
}
}
英文:
Before spring boot in the core, we need to deal with application context
then from there, we have to take the next running service class bean than call that method.
There also have two ways for XmlBaseConfiguration or AnnotationBaseConfiguration
public class XmlBaseConfiguration {
public static void main(String[] args) {
ApplicationContext appContext = new ClassPathXmlApplicationContext(
"META-INF/spring-context.xml");
HelloWorld hw = (HelloWorld) appContext.getBean("helloWorld");
String output = hw.getValueFromContext("strHelloWorld");
System.out.print(output);
}
}
OR
public class AnnotationBaseConfiguration {
public static void main(String[] args) {
ApplicationContext appContext = new AnnotationConfigApplicationContext(Configuration.class);
HelloWorld hw = (HelloWorld) appContext.getBean("helloWorld");
String output = hw.getValueFromContext("strHelloWorld");
System.out.print(output);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论