springBoot入门

1、springBoot是什么
在我们使用SSM开发的时候,我们需要将SSM以xml配置的形式进行整合,同时会发现spring本身的配置就占了很大部分、会导致我们需要对项目的配置文件的进行分类管理,这个时候springBoot就应运而生,专门帮我们管理配置文件。
但是实际上现在springBoot已经发展成了一种开发体系,集成了我们的spring的核心工厂,springMVC、JPA、还有一些spring的功能,同时集成了一个tomcat服务器。我们只需要利用一个springBoot就可以完成整套spring应用的开发,

2、springBoot入门案例
提示:springBoot建议在maven的模式下开发
springBoot集成了我们spring的系列产品,所以我们导包只需要导入一个springBoot就可以了

<project xmlnsprojectxmlns="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.lhc.springbootjpa</groupId>  
 <artifactId>SpringBoot_jpa_test1</artifactId>  
  <version>0.0.1-SNAPSHOT</version>  

  <parent>  
   <groupId>org.springframework.boot</groupId>  
   <artifactId>spring-boot-starter-parent</artifactId>  
   <version>1.5.2.RELEASE</version>  
</parent>  
  <dependencies>  
    <dependency>  
      <groupId>junit</groupId>  
      <artifactId>junit</artifactId>  
      <version>3.8.1</version>  
      <scope>test</scope>  
    </dependency>  
    <dependency>  
       <groupId>org.springframework.boot</groupId>  
       <artifactId>spring-boot-starter-web</artifactId>  
    </dependency>  

    <!-- Spring boot 引用Thymeleaf模板依赖包(Thymeleaf模板如果不适用,这里也可以不添加这段配置,Thymeleaf模板使用在下面会讲到) -->  
<dependency>  
           <groupId>org.springframework.boot</groupId>  
           <artifactId>spring-boot-starter-thymeleaf</artifactId>  
</dependency>  

  </dependencies>  
</project> 

编写Application类
1、新建项目的包结构
这里写图片描述

注意:【我们在com.xingxue下面新建Application类,这样就相当于springBoot默认扫描Application类所在所有包】
在resources下新建application.properties文件,里面是服务器的配置,比如修改端口号
这里写图片描述

package com.xingxue;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;


@SpringBootApplication
@Controller
@RequestMapping("/test")
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class);
    }
    @RequestMapping("test.do")
    public String test() {
        System.out.println("1111111111111");
        return "ok";
    }
}

直接右键以java程序运行即可,结果如下:

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

2017-12-07 14:40:54.337  INFO 4136 --- [           main] com.xingxue.Application                  : Starting Application on PC-20170521BQHB with PID 4136 (D:\wp_heyi2\SpringBoot\target\classes started by Administrator in D:\wp_heyi2\SpringBoot)
2017-12-07 14:40:54.339  INFO 4136 --- [           main] com.xingxue.Application                  : No active profile set, falling back to default profiles: default
2017-12-07 14:40:54.616  INFO 4136 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@1b853a5: startup date [Thu Dec 07 14:40:54 CST 2017]; root of context hierarchy
2017-12-07 14:40:55.678  INFO 4136 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8089 (http)
2017-12-07 14:40:55.689  INFO 4136 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2017-12-07 14:40:55.690  INFO 4136 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.11
2017-12-07 14:40:55.818  INFO 4136 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2017-12-07 14:40:55.818  INFO 4136 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1203 ms
2017-12-07 14:40:55.939  INFO 4136 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2017-12-07 14:40:55.944  INFO 4136 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-12-07 14:40:55.945  INFO 4136 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-12-07 14:40:55.949  INFO 4136 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-12-07 14:40:55.950  INFO 4136 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2017-12-07 14:40:56.264  INFO 4136 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@1b853a5: startup date [Thu Dec 07 14:40:54 CST 2017]; root of context hierarchy
2017-12-07 14:40:56.321  INFO 4136 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/test/test.do]}" onto public java.lang.String com.xingxue.Application.test()
2017-12-07 14:40:56.324  INFO 4136 --- [           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)
2017-12-07 14:40:56.324  INFO 4136 --- [           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)
2017-12-07 14:40:56.347  INFO 4136 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-12-07 14:40:56.347  INFO 4136 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-12-07 14:40:56.392  INFO 4136 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-12-07 14:40:56.418  WARN 4136 --- [           main] .t.AbstractTemplateResolverConfiguration : Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)
2017-12-07 14:40:56.843  INFO 4136 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2017-12-07 14:40:56.889  INFO 4136 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8089 (http)
2017-12-07 14:40:56.895  INFO 4136 --- [           main] com.xingxue.Application                  : Started Application in 2.839 seconds (JVM running for 3.092)

输入地址 http://localhost:8080/test/test.do
我们发现控制台打印 如下
这里写图片描述

这时表明springboot已经运行,这个过程是:
首先springboot自带tomcat,当我们启动的时候,springboot就把该项目自动发布到自带的tomcat下面,然后默认使用8080端口启动tomcat,项目就已经发布成功,
我们访问的时候直接根据原来的方式访问即可,这时发现请求到达了controller,而我们注解实际上是使用springMVC注解,
总结一句话,相当于我们使用了springBoot,springBoot就默认帮我们生成了springMVC和spring的配置文件,而且自动帮我们发布项目

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot是一个用于快速构建基于Spring框架的应用程序的开源框架。它提供了一种简化的方法来配置和部署应用程序,使开发人员能够更快地开发和运行应用程序。 Spring Boot Actuator是Spring Boot的一个组件,它可以帮助我们监控和管理Spring Boot应用程序,包括健康检查、审计、统计和HTTP追踪等功能。要使用Spring Boot Actuator,只需引入相应的起步依赖,并在应用程序的入口点类上添加@SpringBootApplication注解即可。在该类中,使用@SpringBootApplication注解相当于同时添加了@Configuration、@EnableAutoConfiguration和@ComponentScan注解,它标识了当前应用程序是一个Spring Boot应用程序。要启动Spring Boot应用程序,只需在主启动类中编写main函数,通过调用SpringApplication.run(Application.class, args)方法来启动应用程序。在开发过程中,<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [SpringBoot入门](https://blog.csdn.net/weixin_45905210/article/details/121712027)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* [spring boot 入门](https://blog.csdn.net/zhshx19900318/article/details/129476812)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值