zoukankan      html  css  js  c++  java
  • SpringBoot搭建非web应用的两种方式

    方式一:

    在 直接在 main 方法中,根据 SpringApplication.run() 方法获取返回的 Spring 容器对象context上下文,再获取业务 bean 进行调用。

    @SpringBootApplication
    public class Application {
        public static void main(String[] args) {
            /**
             * SpringBoot程序启动后,返回值是ConfigurableApplicationContext,它也是一个Spring容器
             * 它其实相当于原来Spring容器中的ClasspathXmlApplicationContext
             */
    
            // 获取SpringBoot容器
            ConfigurableApplicationContext applicationContext = SpringApplication.run(Application.class, args);
    
            // 从Spring容器中获取指定的对象
            StudentService studentService = (StudentService) applicationContext.getBean("studentServiceImpl");
            BrokerConfig brokerConfig = applicationContext.getBean(BrokerConfig.class);
    
            // 调用业务方法
            String str = studentService.sayHello();
            System.out.println("str = " + str);
        }
    }
    

    方式二:

    Spring boot 的入口类实现 CommandLineRunner 接口

    @SpringBootApplication
    public class App implements CommandLineRunner {
        @Autowired
        BrokerConfig config;
    
        @Override
        public void run(String... args) throws Exception {
            System.out.println(config.getPort());
        }
    
        public static void main(String[] args) {
            SpringApplication.run(App.class, args);
        }
    }
    
    © 2017-2020 版权属于 QXQZX &
  • 相关阅读:
    UVa 820 因特网带宽(最大流)
    UVa 1001 奶酪里的老鼠(Dijkstra或Floyd)
    UVa 821 网页跳跃(Floyd)
    UVa 11624 大火蔓延的迷宫
    UVa 10881 蚂蚁
    UVa 11300 分金币
    UVa 11729 突击战
    《额尔古纳河右岸》读书笔记
    HDU 1083 Courses(二分图匹配模板)
    UVa 10618 跳舞机
  • 原文地址:https://www.cnblogs.com/devhg/p/15783908.html
Copyright © 2011-2022 走看看