二、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

相关文章

Spring boot整合dubbo+zookeeper的详细过程

《Springboot整合dubbo+zookeeper的详细过程》本文讲解SpringBoot整合Dubbo与Zookeeper实现API、Provider、Consumer模式,包含依赖配置、... 目录Spring boot整合dubbo+zookeeper1.创建父工程2.父工程引入依赖3.创建ap

Linux下进程的CPU配置与线程绑定过程

《Linux下进程的CPU配置与线程绑定过程》本文介绍Linux系统中基于进程和线程的CPU配置方法,通过taskset命令和pthread库调整亲和力,将进程/线程绑定到特定CPU核心以优化资源分配... 目录1 基于进程的CPU配置1.1 对CPU亲和力的配置1.2 绑定进程到指定CPU核上运行2 基于

SpringBoot结合Docker进行容器化处理指南

《SpringBoot结合Docker进行容器化处理指南》在当今快速发展的软件工程领域,SpringBoot和Docker已经成为现代Java开发者的必备工具,本文将深入讲解如何将一个SpringBo... 目录前言一、为什么选择 Spring Bootjavascript + docker1. 快速部署与

Spring Boot spring-boot-maven-plugin 参数配置详解(最新推荐)

《SpringBootspring-boot-maven-plugin参数配置详解(最新推荐)》文章介绍了SpringBootMaven插件的5个核心目标(repackage、run、start... 目录一 spring-boot-maven-plugin 插件的5个Goals二 应用场景1 重新打包应用

SpringBoot+EasyExcel实现自定义复杂样式导入导出

《SpringBoot+EasyExcel实现自定义复杂样式导入导出》这篇文章主要为大家详细介绍了SpringBoot如何结果EasyExcel实现自定义复杂样式导入导出功能,文中的示例代码讲解详细,... 目录安装处理自定义导出复杂场景1、列不固定,动态列2、动态下拉3、自定义锁定行/列,添加密码4、合并

Spring Boot集成Druid实现数据源管理与监控的详细步骤

《SpringBoot集成Druid实现数据源管理与监控的详细步骤》本文介绍如何在SpringBoot项目中集成Druid数据库连接池,包括环境搭建、Maven依赖配置、SpringBoot配置文件... 目录1. 引言1.1 环境准备1.2 Druid介绍2. 配置Druid连接池3. 查看Druid监控

Java中读取YAML文件配置信息常见问题及解决方法

《Java中读取YAML文件配置信息常见问题及解决方法》:本文主要介绍Java中读取YAML文件配置信息常见问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 目录1 使用Spring Boot的@ConfigurationProperties2. 使用@Valu

创建Java keystore文件的完整指南及详细步骤

《创建Javakeystore文件的完整指南及详细步骤》本文详解Java中keystore的创建与配置,涵盖私钥管理、自签名与CA证书生成、SSL/TLS应用,强调安全存储及验证机制,确保通信加密和... 目录1. 秘密键(私钥)的理解与管理私钥的定义与重要性私钥的管理策略私钥的生成与存储2. 证书的创建与

浅析Spring如何控制Bean的加载顺序

《浅析Spring如何控制Bean的加载顺序》在大多数情况下,我们不需要手动控制Bean的加载顺序,因为Spring的IoC容器足够智能,但在某些特殊场景下,这种隐式的依赖关系可能不存在,下面我们就来... 目录核心原则:依赖驱动加载手动控制 Bean 加载顺序的方法方法 1:使用@DependsOn(最直

SpringBoot中如何使用Assert进行断言校验

《SpringBoot中如何使用Assert进行断言校验》Java提供了内置的assert机制,而Spring框架也提供了更强大的Assert工具类来帮助开发者进行参数校验和状态检查,下... 目录前言一、Java 原生assert简介1.1 使用方式1.2 示例代码1.3 优缺点分析二、Spring Fr