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

 

你可能感兴趣的:(Spring,Boot)