除了使用SpringApplication.run来启动Spring Boot之外,还有其他的启动方式。

huangapple go评论69阅读模式
英文:

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);
	}
}

huangapple
  • 本文由 发表于 2020年10月11日 03:23:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/64297383.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定