springboot微服务--04 spring-boot-starter-logging和spirng-boot-starter-web


了解 spring-boot-starter

springboot微框架从两个层面影响微服务开发者:

  • 基于spring框架的“约定优先于配置”理念
  • 提供了丰富的spring-boot-starter自动配置依赖模块

我们可以对干预springboot的配置方式进行分类:命令行参数、系统环境变量、位于文件系统中的配置、位于classpath中的配置文件、固化到代码中的配置项

应用日志和 spring-boot-starter-logging

java的日志有多种多样,从java.util默认提供的日志支持,到log4j、log4j2、commons logging等,应用日志系统的配置会比较特殊,从而spring-boot-starter-logging的配置也会比较特殊。

1、需要在maven中添加如下配置中之一即可:

#默认的logging
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-logging</artifactId>
</dependency>

#log4j
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-logg4j</artifactId>
</dependency>

#log4j2
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

SpringBoot应用将自动使用logback作为应用日志框架,SpringBoot启动的时候,由org.springframework.boot.logging.Logging-Application-Listener
根据情况初始化并使用。如果要对SpringBoot提供的日志设定做调整,则可以通过以下两种方式:

  • 01 logback约定,在classpath中使用定制的logbak.xml配置文件;
  • 02 通过application.properties中的logging.config配置中制定自己的配置文件;

web应用开发与spirng-boot-starter-web

#添加springMVC依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

通过在pom.xml中添加以上依赖,就得到了一个直接可执行的web应用,在当前项目下运行mvn spring-boot:run 就可以直接启动一个使用了嵌入式TOMCAT服务请求的web应用,然后在添加一个controller之后就可以实现一个简单的web服务器。简单的背后有很多约定:

项目结构层面约定

对比项springboot传统打包为war的java web
静态文件位置src/main/resources/staticsrc/main/webapp
模板文件位置src/main/resources/templatessrc/main/webapp
SpringMVC 框架层面的约定和定制

spring-boot-starter-web默认会配置一些SpringMVC的必要组件,如下列出一些:
- 必要的ViewResolver,比如ContentNegotiatingViewResolver和BeanNameViewResolver
- 将必要的Converter、GenericConverter和Formatter等bean注册到IOC容器
- 添加一系列HttpMessageConverter以便支持对web请求和相应的类型转换
- 自动配置和注册MessageCodesResolver

注意:任何时候,如果SpringMVC组件不能满足需求,可以在IOC容器中注册同类型的bean来替换,或者直接提供一个基于WebMvcConfigurerAdapter类型的bean定义来定制,甚至可用标注了@EnableWebMvc和@Configuration的配置类来接管SpringMVC的相关配置。

嵌入式Web容器层面的约定和定制

spring-boot-starter-web默认使用嵌入式tomcat作为web容器对外提供HTTP服务,默认使用8080端口对外监听和提供服务,对此都可以实现定制,定制有以下两方面:

01 更换容器和更换端口
更换容器:在pom.xml中编辑

# 01 去除springboot中默认的嵌入式tomcat依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>

# 02 添加jetty依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

更换端口:编辑application.properties文件,添加如下行:

# server.servlet.context-path=/helloboot
# logging.file=D:/mylog/log.log
# logging.level.org.springframework.web= DEBUG
# book.author=wxh
# book.name=20
# debug=true
# spring.profiles.active=dev
server.port=8888

02 对容器内部进行定制

  • 通过向 IOC 容器注册一个EmbeddedServletContainerCustomizer类型的组件来对嵌入式web容器进行定制:
  • 再深入的定制则需要针对特定的嵌入式web容器,使用实现对应的Factory并注册到 IOC 容器:
    • TomcatEnbeddedServletContainerFactory
    • JettyEnbeddedServletContainerFactory
    • UndertowEnbeddedServletContainerFactory

但是通常不要这么做,事实上spring-boot-starter-web提供的配置列表已经是最简单的方式了。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
的区别是什么? 回答: spring-boot-starterspring-boot-starter-web是两个常的spring-boot-starter模块。spring-boot-starter是用于启动和提供日志功能的模块,它包含了一些常的功能和依赖。而spring-bootarter-web则是在-boot-starter的基础添加了Web开发相关的功能,例如处理HTTP请求和响应等。所以如果只需要启动项目或使用日志功能,可以使用spring-boot-starter;如果需要进行Web开发,可以使用spring-boot-starter-web来获得更多的功能和依赖。另外,许多spring-cloud-*的maven依赖中也包含了spring-boot-starter,因此在使用这些依赖时,通常不需要单独引用spring-boot-starter模块。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [spring-boot-starterspring-boot-starter-web的关联](https://blog.csdn.net/m0_67402774/article/details/126505778)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* [spring-boot-starter-loggingspring-boot-starter-web](https://blog.csdn.net/javaheheda/article/details/104974759)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值