SpringBoot01

一、SpringBoot项目中常见的依赖

1.1、spring-boot-starter-parent

        这个是SpringBoot项目必须导入的依赖,这个父模块内部定义了springboot整合各个技术的依赖版本,降低版本的冲突。


        spring-boot-starter-parent
        org.springframework.boot
        2.6.7

1.2、spring-boot-starter-web和spring-boot-starter-test 

        spring-boot-starter-web是springboot项目web开发必须的依赖,spring-boot-starter-test是单元测试的依赖


        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
        

1.3、spring-boot-maven-plugin 

        Springboot项目使用maven进行cleanpackageinstall等操作时必须添加的依赖,否则会在执行上面的操作时报错。 

   
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
   

二、 使用配置文件切换环境

2.1、application.properties是默认配置文件

        我们可以使用配置文件来对springboot项目进行环境切换,启动程序时如果没有指定使用哪个profile配置,那么就会默认使用application.properties配置文件,这是默认配置文件。

        要想使用配置文件切换环境,配置文件必须是application-开头。

        现在以application.propertiesapplication-develop.propertiesapplication-production.properties为例进行操作

        application.properties端口号为:8080

        application-develop.properties端口号为:8081

        application-production.properties端口号为:8082

        SpringBoot01_第1张图片

         目前在application.properties只设置端口号为8080,不指定使用哪个profile配置,并启动程序。结果如下:

SpringBoot01_第2张图片

        可以看到没有指定使用哪个profile配置,那么就会默认使用application.properties配置文件,所以端口为8080

2.2、指定profile配置文件

        在application.properties配置文件中可以指定profile配置文件。通过在application.properties中加上spring.profiles.active=production即可,表示使用application-production.properties配置文件。

   SpringBoot01_第3张图片

 启动程序。结果如下:

SpringBoot01_第4张图片

        可以看到可以通过在application.properties默认配置文件中,加上spring.profiles.active=production来指定配置文件。指定配置文件的前提是配置文件是以application-开头

2.3、通过Idea指定配置文件

2.3.1、Edit Configuration

                选中要更改的springboot项目,并选择Edit Configuration

        SpringBoot01_第5张图片

2.3.2、Active profiles中指定配置文件

        可以在Active profiles中指定配置文件,如下图指定application-develop.properties配置文件,并且使用这种方式比2.2中通过配置文件指定profile优先级要高。

SpringBoot01_第6张图片

 启动程序。结果如下:

SpringBoot01_第7张图片

        可以看到2.2中指定的是application-production.properties,再不改动2.2中配置文件的情况下,在2.3中指定application-develop.properties,最后从结果中可以看到运行的是application-develop.properties,所以在Idea中指定配置文件的优先级是高于通过application.properties配置文件。

2.4、配置文件的优先级

        Springboot项目常见的配置文件有:properties、yml、yaml

        配置文件优先级: properties > yml > yaml 

2.5、配置文件没有被springboot识别怎么办 (配置文件成粉红色)

        有时候配置文件会变成粉红色,没有被SpringBoot项目识别,如下图。

SpringBoot01_第8张图片

        如果配置文件本身没有问题的情况下,实际上是不影响程序的运行。但是要想解决的话,方式如下:

SpringBoot01_第9张图片

SpringBoot01_第10张图片

SpringBoot01_第11张图片

三、SpringBoot内置了Tomcat服务器

        Spring Boot 不再需要将应用部署到 Tomcat 服务器就能运行,这是因为它内嵌了 Tomcat 服务器。

        传统 web 程序和Spring Boot web 程序的区别:

        传统 web 程序,打 war 包,部署至 Tomcat,是 Tomcat 中运行了 Spring 程序 。

        Spring Boot web 程序,打 jar 包,启动内嵌的 Tomcat,是 Spring 程序驱动了 Tomcat      

        优点:控制力更强,像 Filter 等都可以使用 Spring 依赖注入等功能      

        优点:部署也更为方便,不需要单独安装 tomcat,有 java 运行环境即可      

        缺点:jar 包不支持 jsp

    

        将web 容器切换成jetty        SpringBoot01_第12张图片

四、@ServletComponentScan 就可以将 Filter、Servlet 等纳入 Spring 管理 

  启动引导类中的代码:

 在启动引导类上加了  @ServletComponentScan  注解  

package com.itboy;

import com.itboy.Import.MyImportSeletor;
import org.example.config.BrandAutoConfiguration;
import org.example.config.UserAutoConfiguration;
import org.example.pojo.Brand;
import org.example.pojo.User;
import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Import;

/**
 * 
 * 
 */
@SpringBootApplication
@ServletComponentScan //默认扫描引导类所在包及其子包下的servlet组件
public class SpringBootPlusApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext app = SpringApplication.run(SpringBootPlusApplication.class, args);
        
    }
}

对应的Filter

package com.itboy.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

@WebFilter("/*")
public class FilterDemo implements Filter {
    public void init(FilterConfig config) throws ServletException {
    }

    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {

        System.out.println("doFilter运行了..........");

        chain.doFilter(request, response);
    }
}

通过页面访问会发现Filter执行了

SpringBoot01_第13张图片

        说明在启动引导类上添加 @ServletComponentScan  注解就可以将 Filter、Servlet 等纳入 Spring 管理 

你可能感兴趣的:(Spring框架,spring,boot,java,spring,后端)