Springboot (1) Environment construction and demo under idea

Source: Internet
Author: User

1.Spring Boot Introduction

The introduction on the wiki:

Spring Boot is a general configuration solution for spring that creates a standalone, production-level, spring-based application that can "run". It pre-configures spring's "insight view" of the best configuration and use of spring platforms and third-party libraries, so you can start as little as possible. Most spring boot applications require only a small spring configuration. Features:

    • Create a standalone spring application
    • directly embed Tomcat or Jetty(no need to deploy WAR file)
    • provide a self-righteous "beginner" Project object Model (POM) to simplify your MAVEN configuration
    • Automatically configure spring as much as possible
    • provides production-ready features such as metrics , health checks, and external configurations
    • Absolutely no code generation, no XML configuration required
2. Create a project

Create a new MAVEN project: Open idea and select File->new->project as shown:

Locate the MAVEN project as shown in:

Then, fill in the items,

3.pom.xml Configuration

Refer to the Spring website example: https://spring.io/guides/gs/maven/#initial

As follows:

<?XML version= "1.0" encoding= "UTF-8"?><Projectxmlns= "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>Org.springframework</groupId>    <Artifactid>Myspringboot</Artifactid>    <Packaging>Jar</Packaging>    <version>0.1.0</version>    <Properties>        <Maven.compiler.source>1.8</Maven.compiler.source>        <Maven.compiler.target>1.8</Maven.compiler.target>    </Properties>    <Build>        <Plugins>            <plugin>                <groupId>Org.apache.maven.plugins</groupId>                <Artifactid>Maven-shade-plugin</Artifactid>                <version>2.1</version>                <executions>                    <Execution>                        <Phase>Package</Phase>                        <Goals>                            <goal>Shade</goal>                        </Goals>                        <Configuration>                            <Transformers>                                <TransformerImplementation= "Org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">                                    <MainClass>Com.demo.HelloWorld</MainClass>                                </Transformer>                            </Transformers>                        </Configuration>                    </Execution>                </executions>            </plugin>        </Plugins>    </Build></Project>

In addition to optional <packaging> elements, this is the simplest pom.xml file that is necessary to build a Java project. It includes the following details about the project configuration:

    • <modelVersion>。 Pom model version (always 4.0.0).

    • <groupId>。 The group or organization to which the project belongs. Typically expressed as a reverse domain name.

    • <artifactId>。 The name of the project library artifact to give (for example, the name of its jar or war file).

    • <version>。 The version of the project being built.

    • <packaging>-How to package the project. For JAR file packaging, the default is "jar". Use "war" to package the war file.

4. Adding a startup class

To create a package under the Src/main/java path, I named Com.demo. Under the package, create a new Java class named HelloWorld as the startup class.

Helloworld.java Code:

 Package Com.demo; Import org.springframework.boot.SpringApplication; Import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication  Public class HelloWorld {    publicstaticvoidthrows  exception{        Springapplication.run (HelloWorld. class , args);}    }

The @springbootapplication used here needs to be added to the pom.xml in the <project> :

<Dependencies>        <!--Https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -        <Dependency>            <groupId>Org.springframework.boot</groupId>            <Artifactid>Spring-boot-starter-web</Artifactid>            <version>1.5.9.RELEASE</version>        </Dependency>
</Dependencies>

Once added, click on the lower right corner of import changes to download the installation automatically.

5. Writing the business layer

Under the Com.demo path, create a package named Controller, under package to create a Java class named Hellocontroller.

This class is primarily used to interact with pages.

Hellocontroller.java Code:

 Package Com.demo.Controller; Import Org.springframework.stereotype.Controller; Import org.springframework.web.bind.annotation.RequestMapping; Import org.springframework.web.bind.annotation.ResponseBody; @Controller  Public class Hellocontroller {    @RequestMapping ("/hello")    @ResponseBody    public  String Hello () {        return "Hello Spring Boot";    }}

6. Running

Such a simple demo is done. You can start the project by running the main method directly in the startup class, because sping boot has built-in Tomcat

You can see that Tomcat is running on port 8080, and in the browser address bar, enter: Http://localhost:8080/hello, which appears:

At this end, the project runs successfully.

7. Questions
    • WARNING ** : Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package.

       is due to the startup class file, which is placed directly under the Main/java folder.

Ref: 1190000004493460

is caused by a structural problem with the directory

Ref: 53670112

8. Summary

Just contact spring Boot to write a demo to encounter a lot of problems, spring boot as a new technology, may be used later, it seems to continue to learn.

Springboot (1) Environment construction and demo under idea

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.