0
点赞
收藏
分享

微信扫一扫

springboot入门

1. 介绍  7

SpringBoot是Spring中的一个成员, 可以简化Spring,SpringMVC的使用。 他的核心还是IOC容器。

1.1 特点:  7

Create stand-alone Spring applications

创建spring应用

Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)

内嵌的tomcat, jetty , Undertow 

Provide opinionated 'starter' dependencies to simplify your build configuration

提供了starter起步依赖,简化应用的配置。   比如使用MyBatis框架 , 需要在Spring项目中,配置MyBatis的对象 SqlSessionFactory , Dao的代理对象在SpringBoot项目中,在pom.xml里面, 加入一个 mybatis-spring-boot-starter依赖

Automatically configure Spring and 3rd party libraries whenever possible

尽可能去配置spring和第三方库。叫做自动配置(就是把spring中的,第三方库中的对象都创建好,放到容器中, 开发人员可以直接使用)

Provide production-ready features such as metrics, health checks, and externalized configuration

提供了健康检查, 统计,外部化配置

Absolutely no code generation and no requirement for XML configuration

不用生成代码, 不用使用xml,做配置

使用 spring boot 提供的初始化器。 向导的方式,完成 spring boot 项目的创建: 使用方便。

2. 创建Spring Boot项目 9

2.1 第一种方式, 使用Spring提供的初始化器, 就是向导创建SpringBoot应用

使用的地址: https://start.spring.io

springboot入门_注解

springboot入门_springboot_02

选择依赖

springboot入门_注解_03

最后创建项目,设置项目的目录位置

2.1.1 Spring Boot 项目目录结构   10

springboot入门_springboot_04

起步依赖

springboot入门_maven_05

2.1.2 还有一种方式  12

网址https://start.springboot.io/

浏览器配置好springboot,然后下载,再导入idea中

springboot入门_spring_06

2.2 第二种方式,使用 springboot 提供的初始化器, 使用的国内的地址   11

国内地址: https://start.springboot.io

创建项目的步骤同上第二种方式,使用 springboot 提供的初始化器, 使用的国内的地址 

国内地址: https://start.springboot.io

创建项目的步骤同上

springboot入门_spring_07

2.3  第三种方式 使用 maven 向导创建项目    13

创建一个普通 maven 项目

springboot入门_springboot_08

添加 gav

springboot入门_maven_09

点击 finish 创建,完成项目创建。

修改项目的目录

springboot入门_spring_10

添加 Spring Boot 依赖

springboot入门_注解_11

springboot入门_注解_12

创建启动类:加入@SpringBootApplication 注解

springboot入门_注解_13

3. 基于springboot的web例子  14

代码在course2_4中

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.11</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.bjpowernode</groupId>
    <artifactId>course2_4</artifactId>
    <version>1.0.0</version>

    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
<!--        web的起步依赖,springmvc功能-->
        <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>
    </dependencies>

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

</project>


HelloSpringBoot

package com.bjpowernode.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

//基于springboot的web例子  14
@Controller
public class HelloSpringBoot {

    @RequestMapping("/hello")
    @ResponseBody//是为了让返回值String当作数据来用而不是视图
    public String helloSpringBoot(){
        return "欢迎使用SpringBoot框架";
    }
}

Application

package com.bjpowernode;

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

}

springboot入门_maven_14

4. 直接的使用  15

@SpringBootApplication : @SpringBootApplication 是 一 个 复 合 注 解 , 是 由

@SpringBootConfiguration, @EnableAutoConfiguration ,@ComponentScan 联合在一起组成的。

@SpringBootConfiguration : 就 是 @Configuration 这 个 注 解 的 功 能 , 使 用

@SpringBootConfiguration 这个注解的类就是配置文件的作用。

@EnableAutoConfiguration:开启自动配置, 把一些对象加入到 spring 容器中。

@ComponentScan:组件扫描器, 扫描注解,根据注解的功能,创建 java bean,给属性赋值等等。组件扫描器默认扫描的是 @ComponentScan 注解所在的类, 类所在的包和子包。

4.1 @SpringBootConfiguration  15

说明:使用了@SpringBootConfiguration注解标注的类,可以作为配置文件使用的,可以使用Bean声明对象,注入到容器

@Configuration
public @interface SpringBootConfiguration {
    @AliasFor(
        annotation = Configuration.class
    )
    boolean proxyBeanMethods() default true;
}

4.2 @EnableAutoConfiguration  15

启用自动配置, 把java对象配置好,注入到spring容器中。例如可以把mybatis的对象创建好,放入到容器中

4.3 @ComponentScan   15

@ComponentScan 扫描器,找到注解,根据注解的功能创建对象,给属性赋值等等。默认扫描的包: @ComponentScan所在的类所在的包和子包。

举报

相关推荐

0 条评论