程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

SpringBoot框架

发布于2021-05-29 22:20     阅读(875)     评论(0)     点赞(20)     收藏(2)


一、SpringBoot框架

1、SpringBoot介绍

SpringBoot是Spring家族体系中一员。

在这里插入图片描述

Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”.

We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need minimal Spring configuration.

使用Spring Boot可以轻松地创建独立的,基于生产级别的基于Spring的应用程序,您可以“运行”它们。

我们对Spring平台和第三方库持固执己见的观点,因此您可以以最小的麻烦开始使用。大多数Spring Boot应用程序需要最少的Spring配置。

总结:SpringBoot框架可以快速的去构建企业级的项目环境(整合各种jar包、处理各种冲突、项目进行打包、快速发布、几乎进行零配置就可完成一个项目运行等等)

2、SpringBoot特征

Create stand-alone Spring applications

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

Provide opinionated ‘starter’ dependencies to simplify your build configuration

Automatically configure Spring and 3rd party libraries whenever possible

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

Absolutely no code generation and no requirement for XML configuration

  • 创建独立的Spring应用程序
  • 直接嵌入Tomcat,Jetty或Undertow(无需部署WAR文件)
  • 提供自以为是的“入门”依赖项以简化构建配置
  • 尽可能自动配置Spring和3rd Party库
  • 提供可用于生产的功能,例如指标,运行状况检查和外部化配置
  • 完全没有代码生成,也不需要XML配置

在这里插入图片描述

3、SpringBoot好处

Java项目一直以来都有一个严重的问题:

  • 基于xml的配置臃肿,而且不利于团队协作开发。框架的很多核心的配置,其实都是固定的,完全以简化掉。
  • 项目可以会依赖大量的第三方的jar包,jar包之间难免会出现版本冲突现象。

而SpringBoot框架就是解决这些问题。可以让项目几乎完成自动化配置,不需要编写任何的xml文件,同时不用去关心jar包之间的冲突等问题。

很方便的与第三方的一些技术整合:redis、mongoDB、MQ、freemark等等。

二、SpringBoot入门

1、搭建SpringBoot的环境

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

2、添加依赖完成简单测试

2.1、关于maven的父子工程

maven构建项目的环境的时候有三种方式:

  • jar方式:普通的java项目,打包之后是一个jar包。可以将这个jar包发给别人使用。
  • war方式:JavaWEB项目,需要web容器(tomcat)运行,项目打包之后war包。会用到servlet、jsp等技术
  • pom方式:它用在项目构建的过程中,用于进行多个子项目的共性内容管理。但是任何最终完成的项目要么是jar、要么是war。pom方式构建的项目一般都会是项目的父项目。而最终编写功能的jar或war项目会去继承pom方式的项目。

在这里插入图片描述

POM方式的项目中:存放的整个项目中最共性和通用的一些模块。主要是用来管理整个项目的jar包(依赖)。

2.2、SpringBoot的依赖

SpringBoot本身就是一个项目,由Spring官方提供,并且这个项目是以pom方式提供的,其中管理的JavaEE开发中所能用到的几乎所有的依赖,以及服务器。

Spring Boot 2.3.0.RELEASE需要Java 8,并且与Java 14(包括)兼容。 还需要Spring Framework 5.2.6.RELEASE或更高版本。

Build ToolVersion
Maven3.3+
Gradle6 (6.3 or later). 5.6.x is also supported but in a deprecated form

Spring Boot supports the following embedded servlet containers:

NameServlet Version
Tomcat 9.04.0
Jetty 9.43.1
Undertow 2.04.0

You can also deploy Spring Boot applications to any Servlet 3.1+ compatible container.

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.neusoft.springboot</groupId>
    <artifactId>springboot-demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <!--
        导入SpringBoot的项目依赖,SpringBoot是一个pom工程,
        在我们的项目中需要去继承pom工程

        如果一个项目是pom方式,那么在别的项目要使用的话,需要通过parent标签来引用这个这项目
    -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.0.RELEASE</version>
    </parent>

    <dependencies>
        <!-- 需要使用SpringMVC, -->
        <!-- spring-boot-starter-web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

    </dependencies>
</project>

2.3、导入springMVC的启动器

    <!-- 需要使用SpringMVC, -->
    <!-- spring-boot-starter-web -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

2.4、编写SpringBoot启动类

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

2.5、编写Controller

@RestController
@RequestMapping("/demo")
public class DemoController {

    @RequestMapping("/abc")
    public String demo(){
        System.out.println("请求被接受到了.......");
        return "hello world";
    }
}

2.6、启动测试

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

上面这些操作没有任何的xml、tomcat的配置,已经可以完成一个简单的请求和响应的处理,主要是因为在SpringBoot已经为我们做了大量的配置:

在这里插入图片描述

在SpringBoot,已经将SpringMVC的核心对象DispatcherServlet自动配置完成。

在这里插入图片描述

在这里插入图片描述

三、关于SpringBoot的一些配置

1、父工程配置

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在顶层的父工程中配置了大量的依赖和第三方jar包的版本管理

<properties>
  <log4j2.version>2.13.2</log4j2.version>
  <logback.version>1.2.3</logback.version>
  <lombok.version>1.18.12</lombok.version>
  <mockito.version>3.3.3</mockito.version>
  <mongodb.version>4.0.3</mongodb.version>
  <mssql-jdbc.version>7.4.1.jre8</mssql-jdbc.version>
  <mysql.version>8.0.20</mysql.version>
  <spring-boot.version>2.3.0.RELEASE</spring-boot.version>
  <servlet-api.version>4.0.1</servlet-api.version>
  <slf4j.version>1.7.30</slf4j.version>
  <spring-framework.version>5.2.6.RELEASE</spring-framework.version>
  还有大量的版本管理配置.................
</properties>

在maven的项目中,如果一个项目只是管理依赖,而整个项目不真正完成任何的业务逻辑的代码,也就是这个项目中不需要真正去使用任何的类(依赖)。这时使用dependencyManagement来管理所有的依赖,当子工程继承当前这个项目之后,在子工程的pom文件中去使用dependencies来引用已经被父工程管理的依赖即可。

<dependencies>
    <!-- 需要使用SpringMVC, -->
    <!-- spring-boot-starter-web -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
</dependencies>

如果父工程中管理的版本不适合当前子工程使用,这时子工程到导入依赖的时候,可以重新指定版本。

2、SpringBoot的基础配置

SpringBoot框架不提倡使用xml进行项目的配置,过去可能根据SpringMVC、Spring、Mybatis框架,都需要进行一些框架的核心基础配置,以下是Spring框架中的部分配置

<!-- 开启spring的扫描包和注解 -->
<context:annotation-config />
<context:component-scan base-package="com.neusoft" />

<!-- 加载数据库的参数文件 -->
<context:property-placeholder location="classpath:jdbc.properties" />

<!-- 配置数据库连接池 -->
<bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource">
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
    <property name="driverClassName" value="${jdbc.driver}" />
</bean>

SpringBoot不提倡使用xml配置,但是有些参数还是必须配置的。SpringBoot提供自己的一些配置方式:

2.1、在resources目录下进行配置

SpringBoot自己的配置文件默认有两种风格:1、properties;2、yml或yaml

在这里插入图片描述

SpringBoot的核心配置文件的名称是固定的,必须是application,并且扩展名只能是.properties或者.yaml.yml

2.2、SpringBoot读取配置方式

假设编写连接数据库的参数配置:

jdbc.username=root
jdbc.password=root
jdbc.url=jdbc:mysql://127.0.0.1:3306/mybatis
jdbc.driver=com.mysql.jdbc.Driver

导入mysql和driud的依赖

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.27</version>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.22</version>
</dependency>

SpringBoot允许我们在程序中读取一些自己的配置数据的。只是不建议采用xml配置。可以类代替xml的配置,然后在类中定义相关的属性和方法,最终得到想要的对象即可。

// @Configuration 当前的类是个配置类
@Configuration
// @PropertySource 关联外部的文件
@PropertySource("classpath:jdbc.properties")
public class JDBCConfig {
    @Value("${jdbc.username}")
    private String username ;
    @Value("${jdbc.password}")
    private String password;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.driver}")
    private String driver;

    // 最终需要得到连接池,并且交给Spring管理
    // 在SpringBoot中,如果方法返回的对象需要Spring管理,只需要在方法上添加@Bean注解
    @Bean
    public DataSource getDataSource(){
        // 创建阿里巴巴的数据库连接池
        DruidDataSource dataSource = new DruidDataSource();
        // 设置连接数据库的相关参数
        dataSource.setUsername(this.username);
        dataSource.setPassword(this.password);
        dataSource.setUrl(this.url);
        dataSource.setDriverClassName(this.driver);
        return dataSource;
    }
}

编写代码测试

@RequestMapping("/jdbc")
public DataSource demo2(){
    System.out.println(dataSource);
    return dataSource;
}

测试结果:

在这里插入图片描述

在这里插入图片描述

2.3、SpringBoot读取中定义配置第二种方式

2.3.1、配置关联的参数

在这里插入图片描述

/*
 @ConfigurationProperties 注解等价于下面三个何在一起的效果
   @Configuration
   @PropertySource("classpath:jdbc.properties")   // 需要将四个参数配置到springboot自己的文件中
   @Value("${jdbc.username}")

 使用@ConfigurationProperties  注解的时候,需要使用prefix指定配置文件中的前缀
 在类中的属性需要提供对应的get和set方法,属性名一定要和配置文件中的前缀后面的内容相同
 这时SpringBoot就会自动的将参数配置到对象中。
 */
@ConfigurationProperties(prefix = "jdbc")
@Data
public class JDBCData {
    private String username ;
    private String password;
    private String url;
    private String driver;
}

2.3.2、配置bean对象

@Configuration // 这是一个配置类
// 需要关联到设置参数的对象
@EnableConfigurationProperties(value = JDBCData.class)
public class DataSourceConfig {

    @Bean // 返回对象交给Spring管理
    public DataSource getDataSource( JDBCData jdbcData ){
        // 创建阿里巴巴的数据库连接池
        DruidDataSource dataSource = new DruidDataSource();
        // 设置连接数据库的相关参数
        dataSource.setUsername(jdbcData.getUsername());
        dataSource.setPassword(jdbcData.getPassword());
        dataSource.setUrl(jdbcData.getUrl());
        dataSource.setDriverClassName(jdbcData.getDriver());
        return dataSource;
    }
}

四、SpringBoot的核心启动类

1、SpringBootApplication注解

在这里插入图片描述

当搭建SpringBoot项目的时候,一般采用普通的Java项目,然后通过main作为整个项目的入口。在main方法所在的类上需要使用SpringBootApplication注解来标注当前的类是SpringBoot项目的核心启动入口类。

在使用SpringBootApplication注解的方法中,使用main方法,作为程序入口,其中需要使用SpringApplication类中的静态的run方法来让程序运行。

查阅SpringBootApplication的注解源码:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
		@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
    // 注解中的内容省略
}

在SpringBootApplication注解上,又有三个重要的注解:

@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan

在去查阅@SpringBootConfiguration注解的作用:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {
}
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {
}

@SpringBootConfiguration:这个注解来标注某个类是一个SpringBoot的配置类,同时需要将这个类交给Spring管理。

在这里插入图片描述

2、EnableAutoConfiguration注解

EnableAutoConfiguration:主要功能开启自动配置的

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
    
}

在这里插入图片描述

SpringBootApplication注解:第二层含义,自动导入相关的配置(通过隐藏的@AutoConfigurationPackage注解完成,同时在导入的时候需要使用选择器(AutoConfigurationImportSelector)选择性的导入相关的包,主要依赖的是自己项目中的pom引用的starter。

3、ComponentScan注解

ComponentScan注解:相当于配置的Spring的扫描包。让SpringBoot在启动的都扫描哪些包下的类。与在Spring的核心配置文件中配置的下面内容等价:

<!-- 开启spring的扫描包和注解 -->
<context:annotation-config />
<context:component-scan base-package="com.neusoft" />

在这里插入图片描述

其实我们在SpringBoot的项目中,并没有明确的去指定让SpringBoot扫描哪些包,将来交给Spring管理。

只是在核心启动类上使用的了@SpringBootApplication注解(1、配置类;2、自动导入;3、配置扫描包)。

在这里插入图片描述

注意:SpringBoot的核心启动类,建议书写在所有类的公共包下(放在包层级级别较浅的位置下)。

因为在SpringBoot的核心启动类上会使用@SpringBootApplication,这是虽然没有手动指定Spring的扫描包的名称,这时默认扫描的是当前SpringBoot的核心启动类所在的包,以及子包下的所有内容。

以后创建的包的时候:

​ com.neusoft:放SpringBoot的核心启动类

​ com.neusoft.web:

​ com.neusoft.service:

​ com.neusoft.service.impl:

​ com.neusoft.dao(mapper|repository):

​ com.neusoft.utils:

​ com.neusoft.pojo:

4、自动配置的启动器starter

在这里插入图片描述

在SpringBoot的自动配置的jar包中:

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\
org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\
org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration,\
org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration,\
org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration,\
org.springframework.boot.autoconfigure.context.LifecycleAutoConfiguration,\
org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration,\
org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfiguration,\
org.springframework.boot.autoconfigure.dao.PersistenceExceptionTranslationAutoConfiguration,\
org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.cassandra.CassandraReactiveDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.cassandra.CassandraReactiveRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.cassandra.CassandraRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.couchbase.CouchbaseDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.couchbase.CouchbaseReactiveDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.couchbase.CouchbaseReactiveRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.couchbase.CouchbaseRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.elasticsearch.ReactiveElasticsearchRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.elasticsearch.ReactiveElasticsearchRestClientAutoConfiguration,\
org.springframework.boot.autoconfigure.data.jdbc.JdbcRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.ldap.LdapRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.mongo.MongoReactiveDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.mongo.MongoReactiveRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.neo4j.Neo4jDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.neo4j.Neo4jRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.solr.SolrRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.r2dbc.R2dbcDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.r2dbc.R2dbcRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.r2dbc.R2dbcTransactionManagerAutoConfiguration,\
org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration,\
org.springframework.boot.autoconfigure.data.redis.RedisReactiveAutoConfiguration,\
org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.rest.RepositoryRestMvcAutoConfiguration,\
org.springframework.boot.autoconfigure.data.web.SpringDataWebAutoConfiguration,\
org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchRestClientAutoConfiguration,\
org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration,\
org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration,\
org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAutoConfiguration,\
org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration,\
org.springframework.boot.autoconfigure.h2.H2ConsoleAutoConfiguration,\
org.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfiguration,\
org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration,\
org.springframework.boot.autoconfigure.hazelcast.HazelcastJpaDependencyAutoConfiguration,\
org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration,\
org.springframework.boot.autoconfigure.http.codec.CodecsAutoConfiguration,\
org.springframework.boot.autoconfigure.influx.InfluxDbAutoConfiguration,\
org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration,\
org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration,\
org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.JndiDataSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.XADataSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,\
org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration,\
org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration,\
org.springframework.boot.autoconfigure.jms.JndiConnectionFactoryAutoConfiguration,\
org.springframework.boot.autoconfigure.jms.activemq.ActiveMQAutoConfiguration,\
org.springframework.boot.autoconfigure.jms.artemis.ArtemisAutoConfiguration,\
org.springframework.boot.autoconfigure.jersey.JerseyAutoConfiguration,\
org.springframework.boot.autoconfigure.jooq.JooqAutoConfiguration,\
org.springframework.boot.autoconfigure.jsonb.JsonbAutoConfiguration,\
org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration,\
org.springframework.boot.autoconfigure.availability.ApplicationAvailabilityAutoConfiguration,\
org.springframework.boot.autoconfigure.ldap.embedded.EmbeddedLdapAutoConfiguration,\
org.springframework.boot.autoconfigure.ldap.LdapAutoConfiguration,\
org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration,\
org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration,\
org.springframework.boot.autoconfigure.mail.MailSenderValidatorAutoConfiguration,\
org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration,\
org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration,\
org.springframework.boot.autoconfigure.mongo.MongoReactiveAutoConfiguration,\
org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration,\
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\
org.springframework.boot.autoconfigure.quartz.QuartzAutoConfiguration,\
org.springframework.boot.autoconfigure.r2dbc.R2dbcAutoConfiguration,\
org.springframework.boot.autoconfigure.rsocket.RSocketMessagingAutoConfiguration,\
org.springframework.boot.autoconfigure.rsocket.RSocketRequesterAutoConfiguration,\
org.springframework.boot.autoconfigure.rsocket.RSocketServerAutoConfiguration,\
org.springframework.boot.autoconfigure.rsocket.RSocketStrategiesAutoConfiguration,\
org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration,\
org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration,\
org.springframework.boot.autoconfigure.security.servlet.SecurityFilterAutoConfiguration,\
org.springframework.boot.autoconfigure.security.reactive.ReactiveSecurityAutoConfiguration,\
org.springframework.boot.autoconfigure.security.reactive.ReactiveUserDetailsServiceAutoConfiguration,\
org.springframework.boot.autoconfigure.security.rsocket.RSocketSecurityAutoConfiguration,\
org.springframework.boot.autoconfigure.security.saml2.Saml2RelyingPartyAutoConfiguration,\
org.springframework.boot.autoconfigure.sendgrid.SendGridAutoConfiguration,\
org.springframework.boot.autoconfigure.session.SessionAutoConfiguration,\
org.springframework.boot.autoconfigure.security.oauth2.client.servlet.OAuth2ClientAutoConfiguration,\
org.springframework.boot.autoconfigure.security.oauth2.client.reactive.ReactiveOAuth2ClientAutoConfiguration,\
org.springframework.boot.autoconfigure.security.oauth2.resource.servlet.OAuth2ResourceServerAutoConfiguration,\
org.springframework.boot.autoconfigure.security.oauth2.resource.reactive.ReactiveOAuth2ResourceServerAutoConfiguration,\
org.springframework.boot.autoconfigure.solr.SolrAutoConfiguration,\
org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration,\
org.springframework.boot.autoconfigure.task.TaskSchedulingAutoConfiguration,\
org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration,\
org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration,\
org.springframework.boot.autoconfigure.transaction.jta.JtaAutoConfiguration,\
org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration,\
org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration,\
org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration,\
org.springframework.boot.autoconfigure.web.reactive.HttpHandlerAutoConfiguration,\
org.springframework.boot.autoconfigure.web.reactive.ReactiveWebServerFactoryAutoConfiguration,\
org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration,\
org.springframework.boot.autoconfigure.web.reactive.error.ErrorWebFluxAutoConfiguration,\
org.springframework.boot.autoconfigure.web.reactive.function.client.ClientHttpConnectorAutoConfiguration,\
org.springframework.boot.autoconfigure.web.reactive.function.client.WebClientAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.HttpEncodingAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration,\
org.springframework.boot.autoconfigure.websocket.reactive.WebSocketReactiveAutoConfiguration,\
org.springframework.boot.autoconfigure.websocket.servlet.WebSocketServletAutoConfiguration,\
org.springframework.boot.autoconfigure.websocket.servlet.WebSocketMessagingAutoConfiguration,\
org.springframework.boot.autoconfigure.webservices.WebServicesAutoConfiguration,\
org.springframework.boot.autoconfigure.webservices.client.WebServiceTemplateAutoConfiguration

在SpringBoot的自动配置中spring.factories已经编写大量可以自动完成配置的类。但是这些不是说在项目启动的时候就全部配置,是要根据项目导入的starter来决定最后这些自动配置中哪些生效,哪些失效。

What’s in a name
All official starters follow a similar naming pattern; spring-boot-starter-*, where * is a
particular type of application. This naming structure is intended to help when you need to find a
starter. The Maven integration in many IDEs lets you search dependencies by name. For example,
with the appropriate Eclipse or STS plugin installed, you can press ctrl-space in the POM editor
and type “spring-boot-starter” for a complete list.
As explained in the “Creating Your Own Starter” section, third party starters should not start
with spring-boot, as it is reserved for official Spring Boot artifacts. Rather, a third-party
starter typically starts with the name of the project. For example, a third-party starter project
called thirdpartyproject would typically be named thirdpartyproject-spring-boot-
starter.

五、SpringBoot实践

1、SpringBoot整合SSM

在这里插入图片描述

导入依赖:

<?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 http://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.3.0.RELEASE</version>
    </parent>

    <groupId>com.neusoft.edu</groupId>
    <artifactId>ssm-edu</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!-- springmvc的启动器 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- mybatis启动器 -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.3</version>
        </dependency>
        <!-- 通用mapper启动器 -->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>2.1.5</version>
        </dependency>
        <!--pagehelper-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.13</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.27</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.22</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

编写启动类:

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

启动程序报错:

在这里插入图片描述

因为SpringBoot项目中引入数据库和orm框架(mybatis),这时SpringBoot会自动读取连接数据库的初始化4个参数,而项目根本没有做任何的配置。启动肯定报错。

需要配置SpringBoot的核心配置文件:

server:
  port: 80
  servlet:
    context-path: "/edu"
spring:
  application:
    name: edu
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/ssm-demo
    data-username: root
    data-password: root
    driver-class-name: com.mysql.jdbc.Driver

2、SpringBoot处理静态的资源文件

SpringBoot默认要求项目中的所有的静态资源(html、css、js、图片、视频、音频等等文件)需要放在固定的位置,默认在ResourceProperties类中有相关的说明:

在这里插入图片描述



所属网站分类: 技术文章 > 博客

作者:飞人出击

链接:http://www.javaheidong.com/blog/article/207790/b16bbb89aa7e7a96b2bb/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

20 0
收藏该文
已收藏

评论内容:(最多支持255个字符)