springboot入门

SpringBoot概述

SpringBoot提供了一种快速使用Spring的方式,基于约定优于配置的思想,可以让开发人员不必在配置与逻辑业务之间进行思维的切换,全身心的投入到逻辑业务的代码编写中,从而大大提高了开发的效率,一定程度上缩短了项目周期。2014年4月,Spring Boot 1.0.0发布。Spring的顶级项目之一(https://spring.io)。

Spring缺点

在这里插入图片描述

SpringBoot功能

在这里插入图片描述

小结

在这里插入图片描述

SpringBoot快速入门

在这里插入图片描述

在这里插入图片描述

    <!--springboot工程需要继承的父工程-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
    </parent>

    <dependencies>
        <!--web开发的起步依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>


    <repositories>
        <repository>
            <id>aliyun-maven</id>
            <name>aliyun maven</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>aliyun-maven</id>
            <name>aliyun maven</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
        </pluginRepository>
    </pluginRepositories>
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>
/**
 * 引导类。 SpringBoot项目的入口
 */
@SpringBootApplication
public class HelloApplication {

    public static void main(String[] args) {
        SpringApplication.run(HelloApplication.class,args);
    }
}

HelloController

package com.itheima.controller;

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

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String hello(){
        return " hello Spring Boot !";
    }
}

war部署

<packaging>war</packaging>
//继承 SpringBootServletInitializer
@SpringBootApplication
public class HelloApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(HelloApplication.class,args);
    }

	//重写 HelloApplication
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(HelloApplication.class);
    }
}
 <build>
     	<!--指定打包的名称-->
        <finalName>springboot</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

使用Spring Initializr 快速创建项目

在这里插入图片描述

SpringBoot起步依赖原理分析

在这里插入图片描述

SpringBoot配置

配置文件分类

在这里插入图片描述
在这里插入图片描述

yaml

在这里插入图片描述
在这里插入图片描述

基本语法

在这里插入图片描述

数据格式

在这里插入图片描述

参数引用

在这里插入图片描述

小结

在这里插入图片描述

读取配置文件内容(3种)

name: abc

person: 
	name: ${name}
	age: 20
	address: 
		- beijing
		- shanghai

person2: {name: zhangshan,age: 20}

address1: 
	- beijing
	- shanghai


address2: [beijing,shanghai]

msg1: 'hello \n world'
msg2: "hello \n world"

@Value

@Value("${name}")
private String name;

@Value("${person.name}")
private String name2;

@Value("${person.age}")
private int age;

@Value("${address1[0]}")
private String address1;

@Value("${msg2}")
private String msg2;

Environment

@Autowired
private Environment env;


env.getProperty("person.name")
env.getProperty("address[0]")

@ConfigurationProperties

@Component
@ConfigurationProperties(prefix="person")
@Data
public class Person {
	private String name;
	private int age;
	private String[] address;
}

@Autowired
private Person person

String[] address = person.getAddress();

红条提示,添加后再SpringBoot配置文件中编写时就会有提示了

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

过滤器中获取配置文件内容

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        String cas= PlatApplicationContext.getProperty("auth.cas");
        isAuthfilter="true".equals(cas)?true:false;

        ServletContext servletContext = filterConfig.getServletContext();
        WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        userId= ctx.getEnvironment().getProperty("userId");
    }

jsp读取配置文件内容
1.JSP页面引入jstl中的fmt标签

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>

2.使用fmt:setBundle加载properties文件

<!-- Demo为不带properties扩展名的文件名,如Demo.properties;var为存储该配置文件的变量名 -->
<fmt:setBundle basename="Demo" var="demo" />

3.使用fmt:message读取配置值

<!-- key为配置文件中的属性名;var为存储该配置值的变量名;demo为上一步中存储配置文件的变量名 -->
<fmt:message key="hello" var="test" bundle="${demo}" />

4.使用EL表达式读取配置值
5.给js变量赋值,一定要放在单引号中

var hello='${test}'

profile (环境切换)

配置方式

  • 多文件方式
    在这里插入图片描述
  • 多文档方式
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

配置文件加载顺序

  • 项目内加载顺序
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

  • 项目外加载顺序
    命令行参数 如–server.port=8082 以空格分割
    命令行加载指定配置文件 --spring.config.location=e://application.properties
    自动加载与jar同级目录下config下得application.properties
    自动加载与jar同级目录下application.properties

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

悠闲的线程池

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值