springboot应用程序非web方式

package com.cc8w;

import com.cc8w.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class SpringbootAppApplication implements CommandLineRunner {


    @Autowired
    private UserService userService1;


    /**
     * 第一种非Web方法 :
     * 1.获得context容器
     * 2.在获得标注的bean
     * 3.执行bean方法
     */
    public static void main(String[] args) {
        ConfigurableApplicationContext context =
        SpringApplication.run(SpringbootAppApplication.class, args);
        UserService userService = (UserService) context.getBean("userServiceImpl");
        userService.sayHi();

    }



    /**
     * 第二种非Web方法 :
     * 1.启动类集成 CommandLineRunner 接口
     * 2.重写run方法,注解形式@Autowired加入bean
     * 3.重写的run方法可理解为程序入口
     */

    @Override
    public void run(String... args) throws Exception {
        userService1.sayHi();
    }


}
原文地址:https://www.cnblogs.com/fps2tao/p/13965014.html