SpringBoot第一篇:入门

一、对于SpringBoot的理解:

  • SpringBoot设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置(如ssm框架一样)。SpringBoot实际是配置了很多框架的使用方式,就像maven整合了所有的jar包,SpringBoot整合了很多的框架。简化了我们搭建框架的过程。

二、普通SpringWeb项目搭建流程:

          a.配置web.xml,加载Spring和SpringMVC

          b.配置数据库连接、配置Spring事务等(使用mybatis)

          c.配置加载配置文件的读取,开启注解,配置日志记录方式

        d.部署应用服务,调试

    按照上述流程,任何项目功能都需要这样搭建一个框架,大型项目还好说,小型工具(数据加载,统计,下载管理)等如果也都要这样进行搭建,虽然框架可以摘出复用,但是真的很烦。。。恼!

三、SpringBoot HelloWorld

    SpringBoot环境搭建有三种方式:

         a.通过https://start.spring.io/网址进入生成一个SpringBoot环境

         b.通过工具生成SpringBoot环境

         c.手动创建项目环境(我的搭建方式)

    HelloWorld搭建(使用IDEA):

         1、点击File->New->Project->Maven(不用向普通Maven项目一样选择wabapp)->右下角Next->填写GroupId和ArtifactId->Next->填写项目名(Finish)

          2、spring boot jar包版本管理在pom.xml中添加

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

或者

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.1.0.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

  控制spring boot版本,建议使用dependencyManagement方式,因为当分模块开发项目时,父子模块pom文件会存在parent标签,每个pom文件只能存在一个parent标签,故此建议使用dependencyManagement标签。当然也可以单独配置version标签。

3、添加spring boot项目需要的jar(根据自己情况决定)

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

spring-boot-starter-web中包含了MVC和tomcat,还有json等其他依赖jar。

4、在src/java/创建package并创建application类,如:org.wolf.study,App.java

该类中只有一个main方法,其方法内容如下:

SpringApplication.run(Application.class,args);

它时spring boot项目运行的入口。在App类上加@SpringBootApplication注解。或者@EnableAutoConfiguration、@Configuration和@ComponentScan三合一注解。因为两者功能一样。

5、创建一个controller包,并创建一个PageController.java:

@Controller
public class PageController{
  @RequestMapping(value = {"/","index"},method = RequestMethod.GET)
  public String index(){
       return "spring boot hello";
  }
}

6、运行App.java,通过浏览器访问:http://localhost:8080。spring boot内嵌的tomcat端口默认为8080,可通过application.properties修改。

猜你喜欢

转载自blog.csdn.net/FromTheWind/article/details/84298700