二、SpringBoot的配置--bilibili雷丰阳笔记

2024-03-13 20:08

本文主要是介绍二、SpringBoot的配置--bilibili雷丰阳笔记,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1、配置文件

application.properties

application.yml

2、yml

字面量: 数字 字符串 布尔 中间有空格 不加双引号和单引号,双引号(不会转义特殊字符)和单引号(会转义)

k: v

对象:map

friends:lastName: zhangsanage: 20

行内写法

friends: {lastName: zhangsan,age: lisi}

数组:list set

用短横线-数组元素

pets:- cat- dog- pig

行内写法

pets: [cat,dog,pig]

项目

从配置文件获取值

application.yml

person:lastName: zhangsanage: 18boss: falsebirth: 2017/12/12maps: {k1: v1,k2: v2}lists:- lisi- zhangliudog:name: 小狗age: 12

javaBean

package com.it.springboot01helloworld.bean;import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;import java.util.Date;
import java.util.List;
import java.util.Map;/*** 将配置中每个文件的值,映射到这个组件中*/
@Component
@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;@Overridepublic 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 isBoss() {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;}
}package com.it.springboot01helloworld.bean;public class Dog {private String name;private Integer age;@Overridepublic 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;}
}

pom 导入

        <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional></dependency>

测试

package com.it.springboot01helloworld;import com.it.springboot01helloworld.bean.Person;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;/*** 在编码的时候进行容器注入*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootApplicationTest {@AutowiredPerson person;@Testpublic void contextLoads(){System.out.println(person);}
}

application.properties文件

person.last-name=张三
person.age=12
person.birth=2017/12/15
person.boss=false
person.maps.k1=v1
person.maps.k2=v2
person.lists=a,b,c
person.dog.name=dog
person.dog.age=10

在这里插入图片描述

1.如果只需从配置文件获取一个值 @Value("${person.last-name}")

2.如果 javabean和配置文件进行映射 @ConfigurationProperties

3.@PropertySource和@ImportResource

@Component
@ConfigurationProperties(prefix = "person")
//@Validated
@PropertySource(value={"classpath:person.properties"})
public class Person {
//    @Emailprivate String lastName;private Integer age;private boolean boss;private Date birth;private Map<String, Object> maps;private List<Object> lists;private Dog dog;
}

@ImportResource 导入spring配置文件

@SpringBootApplication
@ImportResource(locations = {"classpath:beans.xml"})
public class SpringBoot01HelloworldApplication {public static void main(String[] args) {SpringApplication.run(SpringBoot01HelloworldApplication.class, args);}}/*** 在编码的时候进行容器注入*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootApplicationTest {@AutowiredPerson person;@AutowiredApplicationContext ioc;@Testpublic void testHelloService(){boolean b = ioc.containsBean("helloService");System.out.println(b);}@Testpublic void contextLoads(){System.out.println(person);}
}

beans.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="helloService" class="com.it.springboot01helloworld.service.HelloService"></bean>
</beans>

4.Springboot推荐给容器添加组件

@Configuration
public class MyAppConfig {@Beanpublic HelloService helloService02(){System.out.println("配置类bean给容器添加组件了。。。");return new HelloService();}}

5.配置文件占位符

1.随机数

${random.value} 

2.占位符获取之前获得的值,如果没有可以用:获得默认值

person.last-name=李四${random.uuid}
person.age=12
person.birth=2017/12/15
person.boss=false
person.maps.k1=v1
person.maps.k2=v2
person.lists=a,b,c
person.dog.name=${person.last-name}${person.hello:hello}dog
person.dog.age=10

6.Profile

application-dev.properties

application-prod.properties

spring.profiles.active=dev

yml

server:port: 8090
spring:profiles:active: dev
---
server:port: 8083
spring:profiles: dev
---server:port: 8022
spring:profiles: prod

在启动application

Program arguments的时候添加

--spring.profiles.active=prod

VM options

-Dspring.profiles.active=prod

或者

java -jar filename.jar --spring.profiles.active=prod

7.添加项目背景

server.context-path=/boot

http://localhost:8022/boot/hello

8.还可以通过来改变默认文件的位置

–spring.config.location

项目打包以后,可以通过命令行参数的形式,启动项目来指定

java -jar filename.jar --server.port=8087 --server.context-path=/abc

配置项太多,直接将配置文件放到jar包的旁边

@EnableAutoConfiguration

解释自动配置原理 一个例子

@Configuration
@EnableConfigurationProperties({HttpEncodingProperties.class})
@ConditionalOnWebApplication
@ConditionalOnClass({CharacterEncodingFilter.class})
@ConditionalOnProperty(prefix = "spring.http.encoding",value = {"enabled"},matchIfMissing = true
)
public class HttpEncodingAutoConfiguration { 

所有配置文件的属性都在 xxxProperties中封装的

@ConfigurationProperties(prefix = "spring.http.encoding")
public class HttpEncodingProperties {public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");

根据当前的不同条件判断,决定这个配置类是否生效

springboot会加重大量自动配置类

自动配置类配置了哪些组件

会从properties中获取属性

xxxAutoConfiguration

xxxProperties

idea快捷键 查找某个类

ctrl+shift+n *AutoConfiguration

条件判断类

@ConditionalOnClass(CharacterEncodingFilter.class)

自动配置类在一定条件下才会生效

配置文件 开启debug模式

debug=true

Positive matches:
-----------------DispatcherServletAutoConfiguration matched:- @ConditionalOnClass found required class 'org.springframework.web.servlet.DispatcherServlet'; @ConditionalOnMissingClass did not find unwanted class (OnClassCondition)- @ConditionalOnWebApplication (required) found StandardServletEnvironment (OnWebApplicationCondition)DispatcherServletAutoConfiguration.DispatcherServletConfiguration matched:- @ConditionalOnClass found required class 'javax.servlet.ServletRegistration'; @ConditionalOnMissingClass did not find unwanted class (OnClassCondition)- Default DispatcherServlet did not find dispatcher servlet beans (DispatcherServletAutoConfiguration.DefaultDispatcherServletCondition)Negative matches:
-----------------ActiveMQAutoConfiguration:Did not match:- @ConditionalOnClass did not find required classes 'javax.jms.ConnectionFactory', 'org.apache.activemq.ActiveMQConnectionFactory' (OnClassCondition)AopAutoConfiguration:Did not match:- @ConditionalOnClass did not find required classes 'org.aspectj.lang.annotation.Aspect', 'org.aspectj.lang.reflect.Advice' (OnClassCondition)

这篇关于二、SpringBoot的配置--bilibili雷丰阳笔记的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/806051

相关文章

Nginx 配置跨域的实现及常见问题解决

《Nginx配置跨域的实现及常见问题解决》本文主要介绍了Nginx配置跨域的实现及常见问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来... 目录1. 跨域1.1 同源策略1.2 跨域资源共享(CORS)2. Nginx 配置跨域的场景2.1

javax.net.ssl.SSLHandshakeException:异常原因及解决方案

《javax.net.ssl.SSLHandshakeException:异常原因及解决方案》javax.net.ssl.SSLHandshakeException是一个SSL握手异常,通常在建立SS... 目录报错原因在程序中绕过服务器的安全验证注意点最后多说一句报错原因一般出现这种问题是因为目标服务器

gitlab安装及邮箱配置和常用使用方式

《gitlab安装及邮箱配置和常用使用方式》:本文主要介绍gitlab安装及邮箱配置和常用使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1.安装GitLab2.配置GitLab邮件服务3.GitLab的账号注册邮箱验证及其分组4.gitlab分支和标签的

Java实现删除文件中的指定内容

《Java实现删除文件中的指定内容》在日常开发中,经常需要对文本文件进行批量处理,其中,删除文件中指定内容是最常见的需求之一,下面我们就来看看如何使用java实现删除文件中的指定内容吧... 目录1. 项目背景详细介绍2. 项目需求详细介绍2.1 功能需求2.2 非功能需求3. 相关技术详细介绍3.1 Ja

springboot项目中整合高德地图的实践

《springboot项目中整合高德地图的实践》:本文主要介绍springboot项目中整合高德地图的实践,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一:高德开放平台的使用二:创建数据库(我是用的是mysql)三:Springboot所需的依赖(根据你的需求再

spring中的ImportSelector接口示例详解

《spring中的ImportSelector接口示例详解》Spring的ImportSelector接口用于动态选择配置类,实现条件化和模块化配置,关键方法selectImports根据注解信息返回... 目录一、核心作用二、关键方法三、扩展功能四、使用示例五、工作原理六、应用场景七、自定义实现Impor

SpringBoot3应用中集成和使用Spring Retry的实践记录

《SpringBoot3应用中集成和使用SpringRetry的实践记录》SpringRetry为SpringBoot3提供重试机制,支持注解和编程式两种方式,可配置重试策略与监听器,适用于临时性故... 目录1. 简介2. 环境准备3. 使用方式3.1 注解方式 基础使用自定义重试策略失败恢复机制注意事项

MySQL MCP 服务器安装配置最佳实践

《MySQLMCP服务器安装配置最佳实践》本文介绍MySQLMCP服务器的安装配置方法,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下... 目录mysql MCP 服务器安装配置指南简介功能特点安装方法数据库配置使用MCP Inspector进行调试开发指

SpringBoot整合Flowable实现工作流的详细流程

《SpringBoot整合Flowable实现工作流的详细流程》Flowable是一个使用Java编写的轻量级业务流程引擎,Flowable流程引擎可用于部署BPMN2.0流程定义,创建这些流程定义的... 目录1、流程引擎介绍2、创建项目3、画流程图4、开发接口4.1 Java 类梳理4.2 查看流程图4

一文详解如何在idea中快速搭建一个Spring Boot项目

《一文详解如何在idea中快速搭建一个SpringBoot项目》IntelliJIDEA作为Java开发者的‌首选IDE‌,深度集成SpringBoot支持,可一键生成项目骨架、智能配置依赖,这篇文... 目录前言1、创建项目名称2、勾选需要的依赖3、在setting中检查maven4、编写数据源5、开启热