有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java是启动Spring引导而不是SpringApplication的其他方法。跑

我只是好奇,除了使用SpringApplication之外,还有其他运行或启动SpringBoot应用程序的方法。run()方法。例如,AnnotationConfigApplicationContext可以启动应用程序吗?为什么?你能列出一些其他的方法并加以区分吗


共 (1) 个答案

  1. # 1 楼答案

    在内核中的spring引导之前,我们需要处理应用程序上下文 然后,我们必须使用下一个运行的服务类bean,而不是调用该方法

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