SpringBoot 启动普通java工程

kyaa111 1年前 ⋅ 981 阅读
  1. 引入依赖
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter</artifactId>
  <version>2.0.9</version>
</dependency>
  1. 需要某个类实现CommandLineRunner接口,在run方法内写具体代码
package com.xx.main;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class StartMain implements CommandLineRunner {

    @Autowired
    private IPService ipService;


    /**
     * 实际的入口函数
     *
     * @param args
     * @throws Exception
     */

    public void run(String... args) throws Exception {
	//code
	System.out.println("启动成功");
    }


}

  1. Springboot启动类照旧
package com.xx;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;



@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

运行,控制台输出

启动成功