Spring Boot Combat (first article) first case

Source: Internet
Author: User
Tags abstract bind tomcat apache tomcat
Spring Boot Combat (first article) first case Preface write it in front.
Always wanted to write spring boot related content into a series of blogs, today finally have time to start the first article
will continue to be written after the time.
Spring Boot Blog Content planningSpring boot basic Usage automatic configuration technology integrated performance monitoring source code analysis

Spring boot is powerful and can be followed by a thin path. first Case Construction of the project

There are two ways to build a spring boot project, Gradle, MAVEN, and gradle more simple than Maven's pom configuration, interested students can go to learn gradle, where Maven is used.

Create a MAVEN project that corresponds to the Pom.xml file:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId> spring-boot-starter-parent</artifactid>
        <version>1.2.4.RELEASE</version>
    </parent >

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>

        <dependency>
            <groupId> Org.springframework.boot</groupid>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin >
                <groupId>org.springframework.boot</groupId>
                <artifactId> spring-boot-maven-plugin</artifactid>
            </plugin>
        </plugins>
    </build>

Version with 1.2.4, not the latest version of the official website, but the stable version

Create Application.java

Package com.lkl.springboot;

Import org.springframework.boot.SpringApplication;
Import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication public
class Application {public
    static void Main (string[] args) {
        Springapplication.run (Application.class, args);
    }
}

Run the Main method

  . ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | ||  (_| | ) ) ) )
  '  |____| .__|_| |_|_| |_\__, |  ////=========|_|==============|___/=/_/_/_/:: Spring Boot:: (v1.2.4.release) 2015-08-25 22:53:35.484 INFO 554---[main] com.lkl.springboot.Application:Starting application on mac.local with PID 554 (/users/l Iaokailin/code/github/blog-springboot/target/classes started by Lkl in/users/liaokailin/code/github/ Blog-springboot) 2015-08-25 22:53:35.561 INFO 554---[main] ationconfigembeddedwebapplicationcontext:refres Hing Org.springframework.boot.context.embedded.annotationconfigembeddedwebapplicationcontext@328908cd:startup Date [Tue 22:53:35 CST 2015];     Root of context hierarchy 2015-08-25 22:53:36.395 INFO 554---[main] o.s.b.f.s.defaultlistablebeanfactory : Overriding bean definition for bean ' beannameviewresolver ': replacing [Root bean:class [null]; scope=; abstract=false; lazyinit=false; autowiremode=3; dependencycheck=0; AutowireC Andidate=true; Primary=false; factorybeanname=org.springframework.boot.autoconfigure.web.errormvcautoconfiguration$ Whitelabelerrorviewconfiguration; Factorymethodname=beannameviewresolver; Initmethodname=null; Destroymethodname= (inferred); Defined in class path resource [org/springframework/boot/autoconfigure/web/errormvcautoconfiguration$ Whitelabelerrorviewconfiguration.class]] with [Root bean:class [null]; scope=; abstract=false; lazyinit=false; autowiremode=3; dependencycheck=0; Autowirecandidate=true; Primary=false; factorybeanname=org.springframework.boot.autoconfigure.web.webmvcautoconfiguration$ Webmvcautoconfigurationadapter; Factorymethodname=beannameviewresolver; Initmethodname=null; Destroymethodname= (inferred); Defined in class path resource [org/springframework/boot/autoconfigure/web/webmvcautoconfiguration$ Webmvcautoconfigurationadapter.class]] 2015-08-22:53:37.404 INFO 554---[main] s.b.c.e.t.tomcatembeddedservletcontainer:tomcat initialized with Port (s) : 8080 (http) 2015-08-25 22:53:37.796 INFO 554---[main] o.apache.catalina.core.standardservice:starting  Service Tomcat 2015-08-25 22:53:37.798 INFO 554---[main] org.apache.catalina.core.StandardEngine:Starting Servlet engine:apache tomcat/8.0.23 2015-08-25 22:53:37.955 INFO 554---[ost-startstop-1] o.a.c.c.c.[tomcat]. [localhost]. [/]: Initializing Spring embedded Webapplicationcontext 2015-08-25 22:53:37.955 INFO 554---[ost-startstop-1] O.s . Web.context.ContextLoader:Root Webapplicationcontext:initialization completed in 2397 Ms 2015-08-25 22:53:3 8.813 INFO 554---[ost-startstop-1] o.s.b.c.e.servletregistrationbean:mapping servlet: ' Dispatcherservlet ' to [ /] 2015-08-25 22:53:38.818 INFO 554---[ost-startstop-1] o.s.b.c.embedded.filterregistrationbean:mapping filter: ' Cha Racterencodingfilter ' To: [/*] 2015-08-25 22:53:38.819 INFO 554---[ost-startstop-1] o.s.b.c.embedded.filterregistrationbean:mapping Filte R: ' Hiddenhttpmethodfilter ' to: [/*] 2015-08-25 22:53:39.075 INFO 554---[main] S.w.s.m.m.a.requestmappinghan Dleradapter:looking for @ControllerAdvice: Org.springframework.boot.context.embedded.annotationconfigembeddedwebapplicationcontext@328908cd:startup Date [ Tue 22:53:35 CST 2015];  Root of context hierarchy 2015-08-25 22:53:39.151 INFO 554---[main] s.w.s.m.m.a.requestmappinghandlermapping : Mapped "{[/error],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public Org.springframework.http.responseentity<java.util.map<java.lang.string, java.lang.Object>>
Org.springframework.boot.autoconfigure.web.BasicErrorController.error (Javax.servlet.http.HttpServletRequest) 2015-08-25 22:53:39.151 INFO 554---[main] s.w.s.m.m.a.requestmappinghandlermapping:mapped "{[/error],metho Ds=[],paramS=[],headers=[],consumes=[],produces=[text/html],custom=[]} "onto public Org.springframework.web.servlet.ModelAndView Org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml ( javax.servlet.http.HttpServletRequest) 2015-08-25 22:53:39.180 INFO 554---[main] O.s.w.s.handler.simpleurlha ndlermapping:mapped URL Path [/**] onto handler of type [class Org.springframework.web.servlet.resource.ResourceHttpReq  Uesthandler] 2015-08-25 22:53:39.180 INFO 554---[main] o.s.w.s.handler.simpleurlhandlermapping:mapped URL
Path [/webjars/**] onto handler of type [class Org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2015-08-25 22:53:39.232 INFO 554---[main] o.s.w.s.handler.simpleurlhandlermapping:mapped URL path [/**/fa
Vicon.ico] onto handler of type [class Org.springframework.web.servlet.resource.ResourceHttpRequestHandler]      2015-08-25 22:53:39.326 INFO 554---[main] o.s.j.e.a.annotationmbeanexporter  : Registering beans for JMX exposure on startup 2015-08-25 22:53:39.430 INFO 554---[main] s.b.c.e.t.tomcat Embeddedservletcontainer:tomcat started on port (s): 8080 (http) 2015-08-25 22:53:39.433 INFO 554---[main]
 com.lkl.springboot.Application:Started application in 4.829 seconds (JVM running for 5.238)

Spring boot has been started, with a Tomcat container in it and 8080 ports listening

It's that simple, a spring boot program is created. @SpringBootApplication Annotations

Springbootapplication Annotated source code is as follows:

@Target (Elementtype.type)
@Retention (retentionpolicy.runtime)
@Documented
@Inherited
@ Configuration
@EnableAutoConfiguration
@ComponentScan public
@interface springbootapplication {

    / * *
     Exclude specific auto-configuration classes such that they would never be applied.
     * @return The classes to exclude
     *
    /class<?>[] Exclude () default {};

}

@Configuration: Indicates that application is present as a sprig configuration file
@EnableAutoConfiguration: Start the spring boot built-in automatic configuration
@ComponentScan: Scan the bean, the path is the application class package and the sub-path under the package, here is com.lkl.springboot, in spring The bean in boot is placed under the path already sub-path. Building a Rest project

The above operation even a HelloWorld did not come out, far enough to fill our needs.

Create a Package:com.lkl.springboot.controller Save controller

Build Helloworldcontroller.java

Package Com.lkl.springboot.controller;

Import org.springframework.web.bind.annotation.PathVariable;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.bind.annotation.RequestMethod;
Import Org.springframework.web.bind.annotation.ResponseBody;
Import Org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping ("/springboot") public
class Helloworldcontroller {

    @RequestMapping ( Value = "/{name}", method = requestmethod.get) public
    string Sayworld (@PathVariable ("name") string name) {
        Return "Hello" + Name;
    }
}

Execute application again and then access Http://localhost:8080/springboot/Liaokailin

Get results:
Hello Liaokailin

The annotations involved in the method are not explained, it's simpler ~ The first case is here.

Reprint Please specify
http://blog.csdn.net/liaokailin/article/details/47988617 Welcome attention, your affirmation is my biggest support

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.