In spring boot, to create a non-Web application, implement Commandlinerunner and overwrite the Run () method, for example:
Import Org.springframework.boot.CommandLineRunner;
@SpringBootApplication public
class Springbootconsoleapplication implements Commandlinerunner {
public static void Main (string[] args) throws Exception {
springapplication.run (springbootconsoleapplication.class, args );
}
Access command line arguments
@Override the public
void run (String ... args) throws Exception {
//do something< c9/>}
}
Java
1. Project Structure
A standard MAVEN project structure. As shown below- 2. Project Dependencies
Only rely on the Spring-boot-starter library, refer to the following 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.yiibai</groupId> <artifactid>spring-bo Ot-non-web</artifactid> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging&
Gt
<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.sou rceencoding> </properties> <dependencies> <dependency> <groupid>ju Nit</groupid> <artifactId>junit</artifactId> <version>3.8.1</version>
; <scope>test</scope> </dependency> <dependency> <groupid>org.springframework.boot</group id> <artifactId>spring-boot-starter</artifactId> </dependency> </dependenc ies> <parent> <groupId>org.springframework.boot</groupId> <artifactid>spri ng-boot-starter-parent</artifactid> <version>1.5.1.RELEASE</version> </parent> &L
T;build> <plugins> <!--Package as an executable Jar/war--> <plugin> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-mav
en-plugin</artifactid> </plugin> </plugins> </build> </project>
Xml
3. Spring
Returns the service for the message, as shown in the following Hellomessageservice.java code-
Package com.yiibai.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;
}
}
Java
Property file configuration file: Application.properties as follows-
Name=yiibai
Shell
Here is the Commandlinerunner example, and if you run this spring Boot, then the Run method will be the entry point.
The Springbootconsoleapplication.java code reads as follows-
package Com.yiibai;