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 &
  • 相关阅读:
    html 注释和特殊字符
    html 锚点链接
    html 链接标签
    spring 利用工厂模式解耦
    html 路径
    html 图像标签
    html div和span标签
    html 文本格式化标签
    P5358 [SDOI2019]快速查询
    luoguP2679 子串
  • 原文地址:https://www.cnblogs.com/devhg/p/15783908.html
Copyright © 2011-2022 走看看