SpringBoot2核心技术最好的一篇文章——1.基础入门

本笔记源于观看尚硅谷教学视频,地址:尚硅谷视频

一、Spring与SpringBoot

1.1 SpringBoot的优点

在这里插入图片描述

1.2 SpringBoot缺点

在这里插入图片描述

1.3 详细讲解请观看尚硅谷雷神

地址:雷神讲解

二、SpringBoot2入门

2.1 Maven设置

<mirrors>
    <mirror>
		<id>nexus-aliyun</id>
		<mirrorOf>central</mirrorOf>
		<name>Nexus aliyun</name>
		<url>http://maven.aliyun.com/nexus/content/groups/public</url>
	</mirror>

  </mirrors>

  <profiles>
    
	<profile>
		<id>jdk-1.8</id>
		<activation>
			<activeByDefault>true</activeByDefault>
			<jdk>1.8</jdk>
		</activation>
		<properties>
			<maven.compiler.source>1.8</maven.compiler.source>
			<maven.compiler.target>1.8</maven.compiler.target>
			<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
		</properties>
	</profile>
	
  </profiles>

2.2 HelloSeven

需求: 浏览器发送/hello请求, 响应Hello, SpringBoot2

2.3 创建主程序

@SpringBootApplication : 这是一个SpringBoot应用.(@SpringBootApplication(scanBasePackages = “com.jzq.boot”))
等同于 =>
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(“com.jzq.boot”)

package com.jzq.boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/*
    主程序类
    @SpringBootApplication : 这是一个SpringBoot应用
 */

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

2.4 编写业务

package com.jzq.boot.controller;

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

//@ResponseBody
//@Controller

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String handle01(){
        return "hello seven2";
    }
}

2.5 测试

直接运行MainApplication的Main方法就行!

2.6 简化配置

在资源包创建 application.properties进行配置,例如配置端口号:

server.port=8564

2.7 简化部署

在pom文件加入如下配置,点击Maven的Lifecycle下的clean再点击package。
直接打包为jar包,在控制台运行即可!

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

⭐ 可能会遇到的问题: 取消掉cmd控制台的快速编译模式!

三、了解自动配置管理

3.1 依赖管理

  • ⭐父项目做依赖管理
<--/ 依赖管理 -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.5.6</version>
</parent>

<--/ 它的父项目 -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.5.6</version>
</parent>
<--/ 几乎声明了所有开发中常用的依赖版本号 --->
  • ⭐开发导入starter场景启动器
  1. 见到很多spring-boot-starter-*: * 表示某种场景 例如: aop
  2. 只要引入 starter,这个场景的所有常规需要的依赖我们都自动引入
  3. SpringBoot所有支持的场景: https://docs.spring.io/spring-boot/docs/current/reference/html/using.html#using.build-systems.starters
  4. 见到 *-spring-boot-starter: 第三方为我们提供的简化开发的场景启动器。
  5. 所有场景最底层的依赖:
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
</dependency>
  • ⭐无需关注版本号,自动版本仲裁
  1. 引入依赖默认都可以不写版本。
  2. 引入非版本仲裁的jar或者自己需要指定的版本的jar,要写版本号。
  • ⭐可以修改版本号
  1. 查看spring-boot-dependencies里面规定的当前依赖的版本号,用key例如:<version>${activemq.version}
  2. 在当前项目的pom的 <properties>上写自己需要的项目版本!
<properties>
    <mysql.version>5.1.43</mysql.version>
</properties>

<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

3.2 自动配置

  • ⭐自动配置好Tomcat
    • 引入了tomcat的依赖
  • ⭐自动配置好了SpringMVC
  • ⭐自动配置了Web常见功能,如:字符编码问题
    -⭐ 默认的包结构
    • 自动扫扫描主函数所在以及主函数下面的包
    • 如果自己配置则应该修改主函数的注解

    @SpringBootConfiguration
    @EnableAutoConfiguration
    @ComponentScan(“com.jzq”)
    等同于↓↓↓
    @SpringBootApplication(scanBasePackages = “com.jzq”)

    package com.jzq.boot;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.SpringBootConfiguration;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.annotation.ComponentScan;
    
    /*
        主程序类
        @SpringBootApplication : 这是一个SpringBoot应用
     */
    
    //@SpringBootApplication(scanBasePackages = "com.jzq")
    
    @SpringBootConfiguration
    @EnableAutoConfiguration
    @ComponentScan("com.jzq")
    public class MainApplication {
        public static void main(String[] args) {
            SpringApplication.run(MainApplication.class, args);
        }
    }
    
    
  • ⭐各种配置拥有默认值
    • 默认配置最终都是映射到MultipartProperties
    • 配置文件的值最终会绑定每个类上,这个类会在容器中创建对象
  • ⭐按需加载所有自动配置项
    • 非常多的starter
    • 引入了哪些场景这个场景的自动配置才会开启
    • SpringBoot所有的自动配置功能都在: spring-boot-starter-web依赖的spring-boot-starter中
    <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-autoconfigure</artifactId>
          <version>2.5.6</version>
          <scope>compile</scope>
    </dependency>
    

3.3 容器功能

3.3.1组件添加

⭐1. @Configuration
  • 基本使用
  • Full模式与Lite模式
    • 示例
    • 最佳实战
      • ⭐ 配置类组件之间无依赖关系Lite模式加速容器启动过程,减少判断。(Lite(proxyBeanMethods: false))
      • ⭐ 配置类组件之间有依赖关系Full模式,方法被会调用得到之前单实例组件。可以很方便的进行组件依赖。(Full(proxyBeanMethods: true))

⭐配置类代码:
@Configuration(proxyBeanMethods = true) : // 告诉SpringBoot这是一个配置类 == 配置文件.
配置类本身也是组件.


⭐@Bean : // 给容器中添加组件,以方法名作为组件的id,返回值就是组件类型,返回的值就是组件在容器中的实例.

假如 @Bean(“cat777”), 指定组件名为"cat777"

@Bean
public User user01() {…}

package com.jzq.boot.config;

import com.jzq.boot.bean.Cat;
import com.jzq.boot.bean.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/*
* 1. 配置类里面使用的@Bean标注在方法上给容器注册组件,默认也是单实例的
* 2. 配置类本身也是组件
* 3. proxyBeanMethods: 代理bean的方法
*       Full(proxyBeanMethods: true)
*       Lite(proxyBeanMethods: false)
 */

@Configuration(proxyBeanMethods = true)    // 告诉SpringBoot这是一个配置类  == 配置文件
public class MyConfig {

    @Bean // 给容器中添加组件,以方法名作为组件的id,返回值就是组件类型,返回的值就是组件在容器中的实例
    public User user01() {
        User wxq = new User("wxq");
        wxq.setCat(cat01());
        return wxq;
    }

    @Bean("catttt")
    public Cat cat01() {
        return new Cat(11);
    }
}

⭐2. @Bean、@Component、@Controller、@Service、@Repository

Spring框架介绍了
AOP的注解

⭐3. @ComponentScan、 @Import

@ComponentScan: 包扫描注解
@Import({User.class, DBHelper.class}) 给容器中自动场创建这两个类型的组件,默认组件的名字就是全类名

package com.jzq.boot.config;

import ch.qos.logback.core.db.DBHelper;
import com.jzq.boot.bean.Cat;
import com.jzq.boot.bean.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Import({User.class, DBHelper.class})
@Configuration(proxyBeanMethods = true)    // 告诉SpringBoot这是一个配置类  == 配置文件
public class MyConfig {
    public User user01() {
        User wxq = new User("wxq");
        wxq.setCat(cat01());
        return wxq;
    }

    @Bean("catttt")
    public Cat cat01() {
        return new Cat(11);
    }
}

测试代码与测试图如下:

// 6. 测试@Import()
String[] beanNamesForType = run.getBeanNamesForType(User.class);
System.out.println("s7da7d7sa7das7");
for (String s : beanNamesForType) {
    System.out.println(s);
}
DBHelper bean2 = run.getBean(DBHelper.class);

System.out.println(bean2);
⭐4. @Conditional

条件装配:满足Condittional指定的条件,则进行组件注入
在这里插入图片描述
⭐通过@ConditionalOnBean(name = “catttt”)来指定是否装配,因为该bean装载依赖于另一个bean,假如依赖的catttt没有被转载,该方法也不自动装载;当该方法有catttt时就自动装配。
切记装配顺序,catttt这个组件要在上面!!!
⭐ 当条件装配注解声明在方法上,当条件成立后方法内的配置生效;当注解声明在类上,当条件成立后,类下面的配置才会生效。


@ConditionalOnMissingBean(name=“seven”)
恰恰相反,当容器有seven时不装配,没有seven时才会装配!

package com.jzq.boot.config;

import ch.qos.logback.core.db.DBHelper;
import com.jzq.boot.bean.Cat;
import com.jzq.boot.bean.User;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Import({User.class, DBHelper.class})
@Configuration()    // 告诉SpringBoot这是一个配置类  == 配置文件
public class MyConfig {

    @Bean("catttt")
    public Cat cat01() {
        return new Cat(11);
    }

    @ConditionalOnBean(name = "catttt")
    @Bean // 给容器中添加组件,以方法名作为组件的id,返回值就是组件类型,返回的值就是组件在容器中的实例
    public User user01() {
        User wxq = new User("wxq");
        wxq.setCat(cat01());
        return wxq;
    }
}

⭐通过这个 run.containsBean(“user01”); 测试是否装配了

Boolean c1 = run.containsBean("catttt");
System.out.println(c1);

Boolean u1 = run.containsBean("user01");
System.out.println(u1);

3.3.2 原生配置文件引入

⭐ 1. @ImportResource

@ImportResource(“classpath:bean.xml”): 导入Spring的配置文件,spring通过xml配置的bean也会加载到容器,不导入,不会加载。此注解声明在主配置类。

xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="user11" class="com.jzq.boot.bean.User">
        <property name="name" value="dasdsda"></property>
    </bean>
</beans>

主配置类:

package com.jzq.boot.config;

import ch.qos.logback.core.db.DBHelper;
import com.jzq.boot.bean.Cat;
import com.jzq.boot.bean.User;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportResource;

/*
* 5. @ImportResource("classpath:bean.xml"): 导入Spring的配置文件
 */

@Import({User.class, DBHelper.class})
@Configuration()    // 告诉SpringBoot这是一个配置类  == 配置文件
@ImportResource("classpath:bean.xml")
public class MyConfig {

    @Bean("catttt")
    public Cat cat01() {
        return new Cat(11);
    }

    @ConditionalOnBean(name = "catttt")
    @Bean // 给容器中添加组件,以方法名作为组件的id,返回值就是组件类型,返回的值就是组件在容器中的实例
    public User user01() {
        User wxq = new User("wxq");
        wxq.setCat(cat01());
        return wxq;
    }


}
⭐ 2. 配置绑定 @ConfigurationProperties

使用Java读取到properties文件中的内容,并且把它封装到JavaBean中,以供随时可以使用。
在这里插入图片描述

⭐第一种方式 @ConfigurationProperties(prefix = “cat2”) + @Component()

@Component()通过注解的方式注册bean
@ConfigurationProperties(prefix = “cat2”): 配置绑定properties里的cat2.* 的数据

package com.jzq.boot.bean;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component(value = "cat")
@ConfigurationProperties(prefix = "cat2")
public class Cat {
    private int age;
    private String name;

    public Cat() {}

    public Cat(int age) {
        this.age = age;
    }

    public Cat(int age, String name) {
        this.age = age;
        this.name = name;
    }

    public int getAge() {
        return age;
    }

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

    public String getName() {
        return name;
    }

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

注意 cat2的变量名要和Bean类的成员属性名字一样

server.port=8564
cat2.name="sdsda"
cat2.age=18

测试

 	@Autowired
    Cat cat;
    
    @RequestMapping("/cat")
    public Cat car() {
        return cat;
    }
⭐第二种方式 @ConfigurationProperties(prefix = “cat2”) + @EnableConfigurationProperties

@ConfigurationProperties(prefix = “cat2”) :注解在实体类
@EnableConfigurationProperties:注解在主配置类

package com.jzq.boot.bean;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

//@Component(value = "cat")
@ConfigurationProperties(prefix = "cat2")
public class Cat {
    private int age;
    private String name;

    public Cat() {}

    public Cat(int age) {
        this.age = age;
    }

    public Cat(int age, String name) {
        this.age = age;
        this.name = name;
    }

    public int getAge() {
        return age;
    }

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

    public String getName() {
        return name;
    }

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

@EnableConfigurationProperties: 注解在配置类

package com.jzq.boot.config;

import ch.qos.logback.core.db.DBHelper;
import com.jzq.boot.bean.Cat;
import com.jzq.boot.bean.User;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportResource;

/*
* 1. 配置类里面使用的@Bean标注在方法上给容器注册组件,默认也是单实例的
* 2. 配置类本身也是组件
* 3. proxyBeanMethods: 代理bean的方法
*       Full(proxyBeanMethods: true)
*       Lite(proxyBeanMethods: false)
* 4. @Import({User.class, DBHelper.class})
*       给容器中自动场创建这两个类型的组件,默认组件的名字就是全类名
*
* 5. @ImportResource("classpath:bean.xml"): 导入Spring的配置文件
 */

@Import({User.class, DBHelper.class})
@Configuration()    // 告诉SpringBoot这是一个配置类  == 配置文件
@ImportResource("classpath:bean.xml")
@EnableConfigurationProperties
public class MyConfig {

    @Bean()
    public Cat cat01() {
        return new Cat(11);
    }

    //@ConditionalOnBean(name = "catttt")
    @Bean // 给容器中添加组件,以方法名作为组件的id,返回值就是组件类型,返回的值就是组件在容器中的实例
    public User user01() {
        User wxq = new User("wxq");
        wxq.setCat(cat01());
        return wxq;
    }



}

3.4 自动配置原理入门

⭐在spring-boot-autoconfigure-2.5.6.jar 查看

3.4.1 引导加载自动配置类

⭐ 1. @SpringBootConfiguration

@Configuration: 代表当前是一个配置类。

⭐ 2.@ComponentScan(“com.jzq”)

指定扫描哪些,Spring注解。

⭐ 3. @EnableAutoConfiguration
由
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
组成
  1. @AutoConfigurationPackage

利用Registrar给容器中导入一系列组件
将指定的一个包下的所有组件导入进来?默认时MainApplication

	@Import({Registrar.class})
	public @interface AutoConfigurationPackage {...}
  1. @Import({AutoConfigurationImportSelector.class})

1 利用getAutoConfigurationEntry(annotationMetadata); 给容器中批量导入一些组件;


2 调用List configurations = this.getCandidateConfigurations(annotationMetadata, attributes);获取到所有需要导入到容器的组件。


利用工厂加载
 private static Map<String, List<String>> loadSpringFactories(ClassLoader classLoader) {}

4 从META-INF/spring.factories位置加载一个文件
默认扫描我们当前系统里面的所有META-INF/spring.factories


spring-boot-autoconfigure-2.5.6.jar内也有META-INF/spring.factories
如:

# Initializers
org.springframework.context.ApplicationContextInitializer=\
org.springframework.boot.autoconfigure.SharedMetadataReaderFactoryContextInitializer,\
org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener

...

# Depends on database initialization detectors
org.springframework.boot.sql.init.dependency.DependsOnDatabaseInitializationDetector=\
org.springframework.boot.autoconfigure.batch.JobRepositoryDependsOnDatabaseInitializationDetector,\
org.springframework.boot.autoconfigure.quartz.SchedulerDependsOnDatabaseInitializationDetector,\
org.springframework.boot.autoconfigure.session.JdbcIndexedSessionRepositoryDependsOnDatabaseInitializationDetector

3.4.2 按需开启自动配置项

虽然我们的127个场景的所有自动配置启动的时候会默认全部加载。
但是按照条件装配规则,最终会按需配置。

3.4.3 定制化修改自动配置

在这里插入图片描述

SpringBoot 默认会在底层配置好所有德组件,但是如果用户自己配置了以用户配置的优先

⭐总结:

SpringBoot先加载所有的自动配置类。
每个自动配置类按照条件进行生效。默认都会绑定配置文件指定的值,xxxxProperties里面拿,xxxxProperties和配置文件进行了绑定。
生效的配置类就会给容器中装配很多组件。
只要容器中有这些组件,相当于这些功能就有了。
只要用户有自己的配置,就以用户的优先。

  1. 用户直接自己的@Bean替换底层的组件
  2. 用户去看这个组件是获取的配置文件什么值就去修改。
    xxxAutoConfiguration —> 组件 —> xxxxProperties里面拿值 ----> application.properties

⭐⭐如何自己修改配置步骤

第一种就是看官方文档

⭐⭐第二钟步骤:

  1. 我找到spring-boot-autoconfigure-2.5.6.jar
    在这里插入图片描述
    2.查看@EnableConfigurationProperties({CacheProperties.class})
    在这里插入图片描述
  2. 看注解@ConfigurationProperties(prefix = “spring.cache”)里面的值
    在这里插入图片描述
  3. 在application.properties内修改配置
    在这里插入图片描述

3.4.4最佳实践

  • 引入场景依赖

https://docs.spring.io/spring-boot/docs/current/reference/html/using.html#using.build-systems.starters

  • 查看自动配置了哪些

自己分析,引入场景对应的自动配置一般都生效了。
配置文件中的debug=true开启自动配置报告,Negative(不生效)、Positive(生效)

  • 是否需要修改

参照文档修改配置项:
https://docs.spring.io/spring-boot/docs/current/reference/html/application-properties.html#application-properties.core
或者自己分析。 xxxProperties绑定的配置文件有哪些。


自定义加入或者替换组件:
@Bean、@Component…


自定义器: xxxxCustomizer;

3.4.5 实用技巧

  • dev-tools

配置pom,按ctrl+f9重新部署

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-devtools</artifactId>
     <optional>true</optional>
</dependency>
  • Spring Initailizr(项目初始化向导)
    使用步骤:
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    最后生成的文件目录结构:
    在这里插入图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值