Spring Boot学习笔记

一.Spring Boot之Hello World

1.创建一个maven工程(jar)

选择maven,写项目名,项目路径,结果如下
建立项目后得
在这里插入图片描述

2.导入spring boot依赖

pom.xml


    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.5</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

3.编写一个主程序,并启动Spring Boot应用

package com.chen;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Created by 一只爱吃萝卜的小兔子 on 2021/10/18 23:34
 */

/**
 * 用 @SpringBootApplication 来标注一个主程序类,说明这是一个Spring Boot应用
 */
@SpringBootApplication
public class HelloWorldMainApplication {
    public static void main(String[] args) {
        //Spring应用启动  参数1:主程序类.class  参数2:传入参数args
        SpringApplication.run(HelloWorldMainApplication.class,args);
    }
}

解释:
@SpringBootApplication内容如下:

@SpringBootConfiguration	
@EnableAutoConfiguration
@ComponentScan(
    excludeFilters = {@Filter(
    type = FilterType.CUSTOM,
    classes = {TypeExcludeFilter.class}
), @Filter(
    type = FilterType.CUSTOM,
    classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {

其中

@SpringBootConfiguration	//Spring Boot的配置类;表示这是一个Spring Boot的配置类.含:
		@Configuration配置类上标注这个注解,表示文件是配置类(配置文件)
				配置类也是文件中的一个组件@Component
@EnableAutoConfiguration开启自动配置功能
	以前配置的东西,SpringBoot会帮我们自动配置,;@EnableAutoConfiguration	告诉Spring Boot开启 自动 配置功能.自动配置生效含:
			@AutoConfigurationPackage
		   @Import({AutoConfigurationImportSelector.class})
           public @interface EnableAutoConfiguration
	
			@AutoConfigurationPackage:自动配置包,含
					@Import({Registrar.class})
			**@AutoConfigurationPackage的作用是将主配置类(@SpringBootApplication标准的类)的所在包及包下所有子包里面的所有组件扫描到Spring容器;**

4.编写相关的controller,service

controller包含controller
controller
作用是把用户提交来的请求通过对URL的匹配,分配个不同的接收器,再进行处理,然后向用户返回结果。
重点就在于如何从HTTP请求中获得信息,提取参数,并分发给不同的处理服务。

package com.chen.controller;

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

/**
 * Created by 一只爱吃萝卜的小兔子 on 2021/10/19 14:56
 */

/**
 * controller的作用是把用户提交来的请求通过对URL的匹配,分配个不同的接收器,再进行处理,然后向用户返回结果。
 * 重点就在于如何从HTTP请求中获得信息,提取参数,并分发给不同的处理服务。
 */
@Controller
public class HelloController {

    @ResponseBody //会将return返回结果写给浏览器
    @RequestMapping("/hello")    //接收来自浏览器的hello请求
    public String hello(){
        return "Hello World!";
    }
}

5.启动主程序类的Main,进行测试

根据运行结果:
Tomcat started on port(s): 8080 (http) with context path

在浏览器输入
http://localhost:8080/hello
以上是Hello World的开发

6.打包以及运行jar包

打包前将 下面代码放入pom.xml中

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

打包步骤:
在这里插入图片描述

如果没有以上代码,运行jar包时会出现错误:
spring-boot-01-helloworld-1.0-SNAPSHOT.jar中没有主清单属性


运行jar包
java -jar 包名
在这里插入图片描述



补充:

在这里插入图片描述

场景启动器

Spring-boot-starter-XXX(要写的东西)
xxx场景启动器
在这里插入图片描述

在这里插入图片描述

未完待续

二.快速创建Spring Boot项目

在这里插入图片描述

Server URL改成:

https://start.aliyun.com
在这里插入图片描述
在这里插入图片描述
选择我们需要的模块,项目建立时会联网创建Spring Boot项目
在这里插入图片描述

上图取自
在这里插入图片描述

helloworld

主程序类
主程序类
controller在这里插入图片描述

接口被占用

关闭端口
修改端口
在这里插入图片描述

访问
在这里插入图片描述

3.配置文件

yaml(yml)语法

YAML入门
在这里插入图片描述
选择spring-web依赖,创建项目
在这里插入图片描述

1.JavaBean

1)在(com.chen)包下建bean包,在bean包中建立实体类

其中
@Configuration 表示该类是容器的组件,只有这样容器才支持注解@ConfigurationProperties
@ConfigurationProperties(prefix = “person”) 该类与yml配置文件中的person一一映射
在这里插入图片描述

package com.chen.springboot.bean;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by 一只爱吃萝卜的小兔子 on 2021/10/19 22:51
 */

/**
 * 将application.yml 中的配置的每一个属性的值映射到这个组件中
 * @ConfigurationProperties 告诉Spring Boot将本类中的所有属性和配置文件中相关的配置进行绑定
 * prefix = "person" yml配置文件中的哪个下面的所有属性进行一一映射,此处是person
 *
 * 只有这个组件是容器中的组件(@Configuration),才能容器提供的@ConfigurationProperties功能;
 */
@Configuration
@ConfigurationProperties(prefix = "person")
public class Person {

    private String lastName;
    private Integer age;
    private Boolean boss;
    private Date birth;

    private Map<String, Object> maps;
    private List<Object>  lists;
    private Dog dog;

    @Override
    public String toString() {
        return "Person{" +
                "lastName='" + lastName + '\'' +
                ", age=" + age +
                ", boss=" + boss +
                ", birth=" + birth +
                ", maps=" + maps +
                ", lists=" + lists +
                ", dog=" + dog +
                '}';
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Boolean getBoss() {
        return boss;
    }

    public void setBoss(Boolean boss) {
        this.boss = boss;
    }

    public Date getBirth() {
        return birth;
    }

    public void setBirth(Date birth) {
        this.birth = birth;
    }

    public Map<String, Object> getMaps() {
        return maps;
    }

    public void setMaps(Map<String, Object> maps) {
        this.maps = maps;
    }

    public List<Object> getLists() {
        return lists;
    }

    public void setLists(List<Object> lists) {
        this.lists = lists;
    }

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }
}

上方Person类用到Dog类:
在bean包下创建Dog

package com.chen.springboot.bean;

/**
 * Created by 一只爱吃萝卜的小兔子 on 2021/10/19 22:51
 */
public class Dog {
    private String name;
    private Integer age;

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

2.配置配置文件处理器(依赖)

我们可以导入配置文件处理器,以后编写yml配置就有提示了

<!--导入配置文件处理器,配置文件进行绑定就会有提示-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-configuration-processor</artifactId>
			<optional>true</optional>
		</dependency>

配置文件分为2种:application.yml和application.properties

3.yml

配置文件值注入

1)在配置文件application.yml中写相应配置

**配置文件**
#又名:application.yaml
#端口   person与实体类Person对应
server:
  port: 8081
person:
  lastName: zhangsan
  age: 18
  boss: false
  birth: 2017/12/12
  maps: {k1: v1,k2: 12}
  lists:
    - lisi
    - zhaoliu
  dog:
    name: 小狗
    age: 2

4.properties配置文件

2)或者用配置文件application.properties(一样要先导入配置文件处理器依赖)

# 应用名称
spring.application.name=spring-boot-02-config
# 应用服务 WEB 访问端口
server.port=8080

# 配置person
person.last-name=李四
person.age=12
person.birth=2021/12/12
person.boss=false
person.lists=a,b,c
person.maps.k1=k1
person.maps.k2=k2
person.dog.name=zhangsandog
person.dog.age=21

application.properties中文乱码问题

(properties配置文件在idea中默认utf-8导致乱码)
File→setting,然后如下
在这里插入图片描述

properties测试

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

properties配置文件优先级高于yml,也就是同时存在的情况下,优先执行properties文件

@Value与ConfigurationProperties区别
在这里插入图片描述

5.配置文件注入值数据校验(ConfigurationProperties支持)

@Component
@ConfigurationProperties(prefix = "person")
@Validated
public class Person {

    /**
     * <bean class="Person">
     *      <property name="lastName" value="字面量/${key}从环境变量、配置文件中获取值/#{SpEL}"></property>
     * <bean/>
     */

   //lastName必须是邮箱格式
    @Email
    //@Value("${person.last-name}")
    private String lastName;
    //@Value("#{11*2}")
    private Integer age;
    //@Value("true")
    private Boolean boss;

    private Date birth;
    private Map<String,Object> maps;
    private List<Object> lists;
    private Dog dog;
Value
//赋值name="李四"  这是个定值
@Value("李四");	
public String name;

//赋值name=类.属性  这是个不定值
@Value("${类.属性}");	
public String name;

@PropertySource&@ImportResource&@Bean

1.@PropertySource加载指定配置文件

为了区别于:@ConfigurationProperties(prefix = “person”)默认从全局配置文件中获取值;

说明: 加载指定的配置文件:
用法: 例如加载Person类的配置文件
1)新建person.properties
2)将person的相关配置放到该文件中
3)将application.properties中的Person类的相关配置注释掉
4)回到java文件中的Person类加上 @PropertySource(“classpath:person.properties”)

/**
 * 将配置文件中配置的每一个属性的值,映射到这个组件中
 * @ConfigurationProperties:告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定;
 *      prefix = "person":配置文件中哪个下面的所有属性进行一一映射
 *
 * 只有这个组件是容器中的组件,才能容器提供的@ConfigurationProperties功能;
 *  @ConfigurationProperties(prefix = "person")默认从全局配置文件中获取值;
 *
 */
@PropertySource(value = {"classpath:person.properties"})
@Component
@ConfigurationProperties(prefix = "person")
//@Validated
public class Person {

    /**
     * <bean class="Person">
     *      <property name="lastName" value="字面量/${key}从环境变量、配置文件中获取值/#{SpEL}"></property>
     * <bean/>
     */

   //lastName必须是邮箱格式
   // @Email
    //@Value("${person.last-name}")
    private String lastName;
    //@Value("#{11*2}")
    private Integer age;
    //@Value("true")
    private Boolean boss;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值