这篇博文主要用于新手学习Spring Boot,同时也记录自己学习的过程…
文章内容主要来源于易百教程
在Spring Boot中,要创建一个非Web应用程序,实现CommandLineRunner
并覆盖run()方法,例如:
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootConsoleApplication implements CommandLineRunner {
public static void main(String[] args) throws Exception {
SpringApplication.run(SpringBootConsoleApplication.class, args);
}
//access command line arguments
@Override
public void run(String... args) throws Exception {
//do something
}
}
1.项目结构
一个标准的Maven项目结构。如下所示-
2.项目依赖
只有依赖spring-boot-starter
库,参考如下pom.xml
-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.th</groupId>
<artifactId>spring-boot-non-web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-boot-non-web</name>
<url>http://maven.apache.org</url>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent>
<build>
<plugins>
<!-- Package as an executable jar/war -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.Spring
返回消息的服务,如下 HelloMessageService.java 代码所示 -
package com.th.service;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@Service
public class HelloMessageService {
@Value("${name:unknown}")
private String name;
public String getMessage() {
return getMessage(name);
}
public String getMessage(String name) {
return "Hello " + name;
}
}
属性文件配置文件: application.properties 如下所示 -
name=nicole
下面是CommandLineRunner示例,如果运行这个Spring Boot,那么run方法将是入口点。
SpringBootConsoleApplication.java 代码内容如下所示 –
package com.th;
import static java.lang.System.exit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.Banner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.th.service.HelloMessageService;
@SpringBootApplication
public class SpringBootConsoleApplication implements CommandLineRunner {
@Autowired
private HelloMessageService helloService;
public static void main(String[] args) throws Exception {
//disabled banner, don't want to see the spring logo
SpringApplication app = new SpringApplication(SpringBootConsoleApplication.class);
app.setBannerMode(Banner.Mode.OFF);
app.run(args);
}
// Put your logic here.
@Override
public void run(String... args) throws Exception {
if (args.length > 0) {
System.out.println(helloService.getMessage(args[0].toString()));
} else {
System.out.println(helloService.getMessage());
}
exit(0);
}
}
4.实例运行演示
打包上面的项目并运行它,如下命令-
D:\workspace\spring-boot-non-web>mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building spring-boot-non-web 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:3.0.1:resources (default-resources) @ spring-boot-non-we
b ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 2 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ spring-boot-non-web ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:3.0.1:testResources (default-testResources) @ spring-boo
t-non-web ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\workspace\spring-boot-non-web\src\test\resou
rces
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:testCompile (default-testCompile) @ spring-boot-non
-web ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.21.0:test (default-test) @ spring-boot-non-web ---
[INFO]
[INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ spring-boot-non-web ---
[INFO] Building jar: D:\workspace\spring-boot-non-web\target\spring-boot-non-web-0.0.1-SNA
PSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.0.3.RELEASE:repackage (default) @ spring-boot-non-we
b ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.431 s
[INFO] Finished at: 2018-08-08T15:22:00+08:00
[INFO] Final Memory: 21M/261M
[INFO] ------------------------------------------------------------------------
D:\workspace\spring-boot-non-web>java -jar target/spring-boot-non-web-0.0.1-SNAPSHOT.jar
2018-08-08 15:22:26.903 INFO 6948 --- [ main] com.th.SpringBootConsoleApplicati
on : Starting SpringBootConsoleApplication v0.0.1-SNAPSHOT on SZ10PD0703 with PID 694
8 (D:\workspace\spring-boot-non-web\target\spring-boot-non-web-0.0.1-SNAPSHOT.jar started
by nicoletang in D:\workspace\spring-boot-non-web)
2018-08-08 15:22:26.903 INFO 6948 --- [ main] com.th.SpringBootConsoleApplicati
on : No active profile set, falling back to default profiles: default
2018-08-08 15:22:26.966 INFO 6948 --- [ main] s.c.a.AnnotationConfigApplication
Context : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationCon
text@2db0f6b2: startup date [Wed Aug 08 15:22:26 CST 2018]; root of context hierarchy
2018-08-08 15:22:27.714 INFO 6948 --- [ main] o.s.j.e.a.AnnotationMBeanExporter
: Registering beans for JMX exposure on startup
2018-08-08 15:22:27.730 INFO 6948 --- [ main] com.th.SpringBootConsoleApplicati
on : Started SpringBootConsoleApplication in 1.373 seconds (JVM running for 1.934)
Hello nicole
2018-08-08 15:22:27.730 INFO 6948 --- [ Thread-2] s.c.a.AnnotationConfigApplication
Context : Closing org.springframework.context.annotation.AnnotationConfigApplicationContex
t@2db0f6b2: startup date [Wed Aug 08 15:22:26 CST 2018]; root of context hierarchy
2018-08-08 15:22:27.730 INFO 6948 --- [ Thread-2] o.s.j.e.a.AnnotationMBeanExporter
: Unregistering JMX-exposed beans on shutdown
D:\workspace\spring-boot-non-web>java -jar target/spring-boot-non-web-0.0.1-SNAPSHOT.jar "
11"
2018-08-08 15:23:25.264 INFO 10624 --- [ main] com.th.SpringBootConsoleApplicat
ion : Starting SpringBootConsoleApplication v0.0.1-SNAPSHOT on SZ10PD0703 with PID 10
624 (D:\workspace\spring-boot-non-web\target\spring-boot-non-web-0.0.1-SNAPSHOT.jar starte
d by nicoletang in D:\workspace\spring-boot-non-web)
2018-08-08 15:23:25.264 INFO 10624 --- [ main] com.th.SpringBootConsoleApplicat
ion : No active profile set, falling back to default profiles: default
2018-08-08 15:23:25.327 INFO 10624 --- [ main] s.c.a.AnnotationConfigApplicatio
nContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationCo
ntext@27ddd392: startup date [Wed Aug 08 15:23:25 CST 2018]; root of context hierarchy
2018-08-08 15:23:26.169 INFO 10624 --- [ main] o.s.j.e.a.AnnotationMBeanExporte
r : Registering beans for JMX exposure on startup
2018-08-08 15:23:26.185 INFO 10624 --- [ main] com.th.SpringBootConsoleApplicat
ion : Started SpringBootConsoleApplication in 1.451 seconds (JVM running for 1.999)
Hello 11
2018-08-08 15:23:26.185 INFO 10624 --- [ Thread-2] s.c.a.AnnotationConfigApplicatio
nContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationConte
xt@27ddd392: startup date [Wed Aug 08 15:23:25 CST 2018]; root of context hierarchy
2018-08-08 15:23:26.185 INFO 10624 --- [ Thread-2] o.s.j.e.a.AnnotationMBeanExporte
r : Unregistering JMX-exposed beans on shutdown
D:\workspace\spring-boot-non-web>mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building spring-boot-non-web 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ spring-boot-non-web
---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 2 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ spring-boot-non-web ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ spring-boot-
non-web ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\workspace\spring-boot-non-web\src\test\resou
rces
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ spring-boot-non-w
eb ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ spring-boot-non-web ---
[INFO]
[INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ spring-boot-non-web ---
[INFO] Building jar: D:\workspace\spring-boot-non-web\target\spring-boot-non-web-0.0.1-SNA
PSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:1.5.1.RELEASE:repackage (default) @ spring-boot-non-we
b ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.957 s
[INFO] Finished at: 2018-08-08T15:56:20+08:00
[INFO] Final Memory: 18M/227M
[INFO] ------------------------------------------------------------------------
D:\workspace\spring-boot-non-web>java -jar target/spring-boot-non-web-0.0.1-SNAPSHOT.jar
2018-08-08 15:56:26.211 INFO 9572 --- [ main] com.th.SpringBootConsoleApplicati
on : Starting SpringBootConsoleApplication v0.0.1-SNAPSHOT on SZ10PD0703 with PID 957
2 (D:\workspace\spring-boot-non-web\target\spring-boot-non-web-0.0.1-SNAPSHOT.jar started
by nicoletang in D:\workspace\spring-boot-non-web)
2018-08-08 15:56:26.220 INFO 9572 --- [ main] com.th.SpringBootConsoleApplicati
on : No active profile set, falling back to default profiles: default
2018-08-08 15:56:26.317 INFO 9572 --- [ main] s.c.a.AnnotationConfigApplication
Context : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationCon
text@22d8cfe0: startup date [Wed Aug 08 15:56:26 CST 2018]; root of context hierarchy
2018-08-08 15:56:27.251 INFO 9572 --- [ main] o.s.j.e.a.AnnotationMBeanExporter
: Registering beans for JMX exposure on startup
Hello nicole
2018-08-08 15:56:27.273 INFO 9572 --- [ Thread-2] s.c.a.AnnotationConfigApplication
Context : Closing org.springframework.context.annotation.AnnotationConfigApplicationContex
t@22d8cfe0: startup date [Wed Aug 08 15:56:26 CST 2018]; root of context hierarchy
2018-08-08 15:56:27.274 INFO 9572 --- [ Thread-2] o.s.j.e.a.AnnotationMBeanExporter
: Unregistering JMX-exposed beans on shutdown
D:\workspace\spring-boot-non-web>java -jar target/spring-boot-non-web-0.0.1-SNAPSHOT.jar "
Miss"
2018-08-08 15:56:57.351 INFO 9828 --- [ main] com.th.SpringBootConsoleApplicati
on : Starting SpringBootConsoleApplication v0.0.1-SNAPSHOT on SZ10PD0703 with PID 982
8 (D:\workspace\spring-boot-non-web\target\spring-boot-non-web-0.0.1-SNAPSHOT.jar started
by nicoletang in D:\workspace\spring-boot-non-web)
2018-08-08 15:56:57.361 INFO 9828 --- [ main] com.th.SpringBootConsoleApplicati
on : No active profile set, falling back to default profiles: default
2018-08-08 15:56:57.459 INFO 9828 --- [ main] s.c.a.AnnotationConfigApplication
Context : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationCon
text@22d8cfe0: startup date [Wed Aug 08 15:56:57 CST 2018]; root of context hierarchy
2018-08-08 15:56:58.409 INFO 9828 --- [ main] o.s.j.e.a.AnnotationMBeanExporter
: Registering beans for JMX exposure on startup
Hello Miss
2018-08-08 15:56:58.431 INFO 9828 --- [ Thread-2] s.c.a.AnnotationConfigApplication
Context : Closing org.springframework.context.annotation.AnnotationConfigApplicationContex
t@22d8cfe0: startup date [Wed Aug 08 15:56:57 CST 2018]; root of context hierarchy
2018-08-08 15:56:58.432 INFO 9828 --- [ Thread-2] o.s.j.e.a.AnnotationMBeanExporter
: Unregistering JMX-exposed beans on shutdown
D:\workspace\spring-boot-non-web>
相关文章:
- Spring Boot教程(一)
- Spring Boot是什么?(二)
- Spring Boot主要目标(三)
- 新项目为什么需要Spring Boot?(四)
- Spring Boot核心和限制(五)
- Spring Boot优点和缺点(六)
- Spring Boot入门(七)
- Spring Boot安装(八)
- Spring Boot应用程序开发入门(九)
- Spring Boot JSP应用实例(十)
- Spring Boot将WAR文件部署到Tomcat(十一)
- Spring Boot Hello World(Thymeleaf)示例(十二)
- Spring Boot @ConfigurationProperties实例(十四)
- Spring Boot SLF4J日志实例(十五)
- Spring Boot Ajax实例(十六)