Spring Boot non-Web application instance __web

Source: Internet
Author: User
Tags getmessage

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&gt
            ; <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; 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.