springboot入门之简单demo

项目构建

  我们采用maven构建SpringBoot工程,首先创建一个maven工程,对应的pom文件如下:

<properties>
        <java.version>1.8</java.version>
    </properties>
    <!--集成springboot的父依赖-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.7.RELEASE</version>
    </parent>

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

    <!--打可执行jar包-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

创建Application.java

package com.ysl;

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);
    }
}

运行main方法,运行结果如下:

  .   ____          _            __ _ _
 /\\ / ___‘_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | ‘_ | ‘_| | ‘_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  ‘  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.7.RELEASE)

2018-03-03 10:28:24.028  INFO 6358 --- [           main] com.ysl.Application                      : Starting Application on master with PID 6358 (/home/workspace/springboottest/target/classes started by ysl in /home/workspace/springboottest)
2018-03-03 10:28:24.038  INFO 6358 --- [           main] com.ysl.Application                      : No active profile set, falling back to default profiles: default
2018-03-03 10:28:24.218  INFO 6358 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot[email protected]1b37288: startup date [Sat Mar 03 10:28:24 CST 2018]; root of context hierarchy
2018-03-03 10:28:27.001  INFO 6358 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2018-03-03 10:28:27.025  INFO 6358 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2018-03-03 10:28:27.026  INFO 6358 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.20
2018-03-03 10:28:27.141  INFO 6358 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2018-03-03 10:28:27.142  INFO 6358 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2936 ms
2018-03-03 10:28:27.294  INFO 6358 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: ‘dispatcherServlet‘ to [/]
2018-03-03 10:28:27.299  INFO 6358 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: ‘characterEncodingFilter‘ to: [/*]
2018-03-03 10:28:27.300  INFO 6358 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: ‘hiddenHttpMethodFilter‘ to: [/*]
2018-03-03 10:28:27.300  INFO 6358 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: ‘httpPutFormContentFilter‘ to: [/*]
2018-03-03 10:28:27.300  INFO 6358 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: ‘requestContextFilter‘ to: [/*]
2018-03-03 10:28:27.669  INFO 6358 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot[email protected]1b37288: startup date [Sat Mar 03 10:28:24 CST 2018]; root of context hierarchy
2018-03-03 10:28:27.755  INFO 6358 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-03-03 10:28:27.756  INFO 6358 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" 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)
2018-03-03 10:28:27.795  INFO 6358 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-03-03 10:28:27.796  INFO 6358 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-03-03 10:28:27.835  INFO 6358 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-03-03 10:28:28.143  INFO 6358 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-03-03 10:28:28.252  INFO 6358 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2018-03-03 10:28:28.261  INFO 6358 --- [           main] com.ysl.Application                      : Started Application in 5.148 seconds (JVM running for 5.974)

  spring boot已经启动,内嵌tomcat容器,监听为8080端口,一个springboot程序就这么简单的被创建了。

@SpringBootApplication 注解

SpringBootApplication注解的源码如下:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
    excludeFilters = {@Filter(
    type = FilterType.CUSTOM,
    classes = {TypeExcludeFilter.class}
), @Filter(
    type = FilterType.CUSTOM,
    classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {
    @AliasFor(
        annotation = EnableAutoConfiguration.class,
        attribute = "exclude"
    )
    Class<?>[] exclude() default {};

    @AliasFor(
        annotation = EnableAutoConfiguration.class,
        attribute = "excludeName"
    )
    String[] excludeName() default {};

    @AliasFor(
        annotation = ComponentScan.class,
        attribute = "basePackages"
    )
    String[] scanBasePackages() default {};

    @AliasFor(
        annotation = ComponentScan.class,
        attribute = "basePackageClasses"
    )
    Class<?>[] scanBasePackageClasses() default {};
}

@Configuration : 表示Application作为sprig配置文件存在 
@EnableAutoConfiguration: 启动spring boot内置的自动配置 
@ComponentScan : 扫描bean,路径为Application类所在package以及package下的子路径,这里为 com.ysl,在spring boot中bean都放置在该路径已经子路径下。

构建REST工程

  上面的操作连个HelloWorld都没有出来,远远满不足我们的需求。在com.ysl下创建子包controller,在该包下创建TestController.java,内容如下:

package com.ysl.controller;

import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

    @RequestMapping("/")
    public String home(){
        return "hello,springboot";
    }

}

重新启动程序,访问http:localhost:8080,得到结果:hello,springboot

原文地址:https://www.cnblogs.com/senlinyang/p/8495989.html

时间: 2024-08-07 02:21:39

springboot入门之简单demo的相关文章

SpringBoot入门之简单配置

今天下载了<JavaEE开发的颠覆者SpringBoot实战>这本书,发现Spring还有好多遗漏的部分,算是又恶补了一下,今天主要是学习下SpringBoot的配置. 一.基本配置 1.定制Banner (1).在src/main/resource下新建banner.txt (2).打开http://patorjk.com/software/taag,输入要显示的文字,选择想要的样式,拷贝到banner.txt中,再次启动时就会发现banner已变. (3)关闭banner 可以修改main

springboot测试service简单demo

package com.test.service; import com.task.Application;import com.task.model.po.TaskRecordDo;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.conte

SpringBoot 入门 Demo

SpringBoot   入门 Demo Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置.通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者. 特点 1. 创建独立的Spring应用程序 2. 嵌入的Tomcat,无需部署WAR文件 3. 简化Maven配置

SpringBoot入门十九,简单文件上传

项目基本配置参考SpringBoot入门一,使用myEclipse新建一个SpringBoot项目,使用myEclipse新建一个SpringBoot项目即可.现在来给项目添加一个MyBatis支持,添加方式非常简单,仅需两步即可,具体内容如下: 1. pom.xml添加以下配置信息 <!-- 文件上传配置开始 --> <!-- 9.引入commons-io依赖 --> <dependency> <groupId>commons-io</groupId

Springboot 入门及Demo

一:SpringBoot入门1.1:SpringBoot简介Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置.听说Springboot可以在140个字符以内发布一个web应用.1.2:SpringBoot特性 1. SpringBoot并不是对Spring功能上的增强,而是提供了一种快速创建独立的Spring应用程序的框架2. 嵌入的Tomcat,无需

springboot入门简单,深入难

18年1月份的时候在腾讯课堂学习springboot.springcloud搭建微服务,老师告诉我们,springboot入门容易,深入难. 因为你必须东西SpringMVC.Spring.Mybatis这样的SSM,Redis.SpringData.Rabbitmq.KAFKA等各种各样的组件,然后怎么把它们整合到springboot里面去. 还得配置自动打包.编译,如Jkens,docker等才能将微服务很好的用起来,不然出现分布式的问题事务一致性问题.发布的问题,用微服务是非常痛苦的 原文

SpringBoot入门十八,自定义注解的简单实现

项目基本配置参考文章SpringBoot入门一,使用myEclipse新建一个SpringBoot项目,使用myEclipse新建一个SpringBoot项目即可,此示例springboot升级为2.2.1版本. 1. pom.xml添加aop支持 <!-- 引入aop切面支持 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-

物联网架构成长之路(13)-SpringBoot入门

1. 前言 下载最新版的JavaEE eclipse-jee-oxygen-2-win32-x86_64.zip 安装STS插件 Window->Eclipse Marketplace -> popular 下那个 Spring Tools(aka Spring IDE and Spring Tool Suite) 然后通过STS工具创建一个新的Spring boot工程,这里就不细说了.网上资料很多,也比较简单就可以搭建起来.后面对SpringBoot也只是简单的提一下,还有说一下注意点.没

SpringBoot入门示例

SpringBoot入门Demo SpringBoot可以说是Spring的简化版.配置简单.使用方便.主要有以下几种特点: 创建独立的Spring应用程序 嵌入的Tomcat,无需部署WAR文件 简化Maven配置 自动配置Spring 提供生产就绪型功能,如指标,健康检查和外部配置 绝对没有代码生成和对XML没有要求配置 (1)创建maven项目,构建java项目(注意:这里为java项目,也能通过内嵌的tomcat启动服务,访问controller指定路径返回的数据) 项目目录结构如下: