SpringBoot命令行运行器简单实例

1、控制台窗口Runner是一个接口,可以使用这些接口在应用程序启动后立即执行一些操作。

2、简单实例

(1)创建项目

目录结构

|t
|----src
|--------main
|------------java
|----------------cn
|--------------------hwd
|------------------------t
|----------------------------MySpringBootApplication.java
|------------resources
|--------test
|------------java
|----build.gradle // 配置文件

MySpringBootApplication.java

package cn.hwd.t;

import static java.lang.System.exit;

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

@SpringBootApplication
public class MySpringBootApplication implements CommandLineRunner {

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

	@Override
	public void run(String... args) throws Exception {
		System.out.println("hello world.");
        exit(0);
	}

}

build.gradle

buildscript {
	ext {
		springBootVersion = '1.5.16.RELEASE'
	}
	repositories {
		maven {
			name 't repository'
			url 'http://maven.aliyun.com/nexus/content/groups/public/'
		}
		mavenCentral()
	}
	dependencies {
		classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
	}
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

group = 'cn.hwd.t'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
	maven {
		name 't repository'
		url 'http://maven.aliyun.com/nexus/content/groups/public/'
	}
	mavenCentral()
}

tasks.withType(JavaCompile) {
	options.encoding = "UTF-8"
}

springBoot{
	executable=true
}

dependencies {
	compile('org.springframework.boot:spring-boot-starter')
}

(2)执行gradle eclipse命令并导入eclipse

(3)运行测试


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::       (v1.5.16.RELEASE)

2018-12-10 15:59:19.491  INFO 14592 --- [           main] cn.hwd.t.MySpringBootApplication         : Starting MySpringBootApplication on LAPTOP-N1UHE4RC with PID 14592 (D:\workspace\work\zhongshan\t\bin started by vineg in D:\workspace\work\zhongshan\t)
2018-12-10 15:59:19.496  INFO 14592 --- [           main] cn.hwd.t.MySpringBootApplication         : No active profile set, falling back to default profiles: default
2018-12-10 15:59:19.580  INFO 14592 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@1018bde2: startup date [Mon Dec 10 15:59:19 CST 2018]; root of context hierarchy
2018-12-10 15:59:20.281  INFO 14592 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
hello world.
2018-12-10 15:59:20.291  INFO 14592 --- [       Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@1018bde2: startup date [Mon Dec 10 15:59:19 CST 2018]; root of context hierarchy
2018-12-10 15:59:20.293  INFO 14592 --- [       Thread-2] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值