博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springboot入门之简单demo
阅读量:5320 次
发布时间:2019-06-14

本文共 7311 字,大约阅读时间需要 24 分钟。

项目构建

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

1.8
org.springframework.boot
spring-boot-starter-parent
1.5.7.RELEASE
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-maven-plugin

创建Application.java

package com.ysl;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic 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: default2018-03-03 10:28:24.218  INFO 6358 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@1b37288: startup date [Sat Mar 03 10:28:24 CST 2018]; root of context hierarchy2018-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.202018-03-03 10:28:27.141  INFO 6358 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext2018-03-03 10:28:27.142  INFO 6358 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2936 ms2018-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.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@1b37288: startup date [Sat Mar 03 10:28:24 CST 2018]; root of context hierarchy2018-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
> 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 startup2018-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;@RestControllerpublic class TestController {    @RequestMapping("/")    public String home(){        return "hello,springboot";    }}

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

 

转载于:https://www.cnblogs.com/senlinyang/p/8495989.html

你可能感兴趣的文章
Windows Phone 7你不知道的8件事
查看>>
java的二叉树树一层层输出,Java构造二叉树、树形结构先序遍历、中序遍历、后序遍历...
查看>>
php仿阿里巴巴,php实现的仿阿里巴巴实现同类产品翻页
查看>>
面对问题,如何去分析?(日报问题)
查看>>
nodejs vs python
查看>>
poj-1410 Intersection
查看>>
Java多线程基础(一)
查看>>
SQL Server中利用正则表达式替换字符串
查看>>
POJ 1015 Jury Compromise(双塔dp)
查看>>
论三星输入法的好坏
查看>>
Linux 终端连接工具 XShell v6.0.01 企业便携版
查看>>
JS写一个简单日历
查看>>
Python 发 邮件
查看>>
mysql忘记密码的解决办法
查看>>
全面分析Java的垃圾回收机制2
查看>>
[Code Festival 2017 qual A] C: Palindromic Matrix
查看>>
修改博客园css样式
查看>>
Python3 高阶函数
查看>>
初始面向对象
查看>>
leetcode Letter Combinations of a Phone Number
查看>>