Spring-boot参考(三) 配置

2024-06-22 20:38
文章标签 spring boot 参考 配置 java

本文主要是介绍Spring-boot参考(三) 配置,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

debug

开启debug开关可以查看更多的信息

java -jar myproject-0.0.1-SNAPSHOT.jar –debug
或者
在application.yml 中
debug: true

banner.txt在classpath下增加这个文件,可以替换掉启动Spring的 图案。

Customizing the Banner

配置文件就是 环境变量。

在程序启动时 执行一些方法

实现 ApplicationRunner or CommandLineRunner 接口。

@Component
public class MyBean2 implements ApplicationRunner {@Overridepublic void run(ApplicationArguments args) throws Exception {System.out.println(args);}
}

or

@Component
public class MyBean implements CommandLineRunner {public void run(String... args) {// Do something...System.out.println("hello");System.out.println(args);}}

修改程序退出的 返回值

@SpringBootApplication
public class ExitCodeApplication {@Beanpublic ExitCodeGenerator exitCodeGenerator() {return () -> 42;}public static void main(String[] args) {System.exit(SpringApplication.exit(SpringApplication.run(ExitCodeApplication.class, args)));}}

Spring boot 配置参数的 加载顺序

  1. Devtools global settings properties on your home directory
    (~/.spring-boot-devtools.properties when devtools is active).
  2. @TestPropertySource annotations on your tests.

  3. @SpringBootTest#properties annotation attribute on your tests.

  4. Command line arguments.

  5. Properties from SPRING_APPLICATION_JSON
    (inline JSON embedded in an environment variable or system
  6. ServletConfig init parameters.
  7. ServletContext init parameters.
  8. JNDI attributes from java:comp/env.
  9. Java System properties (System.getProperties()).
  10. OS environment variables.
  11. A RandomValuePropertySource that only has properties in random.*.
  12. Profile-specific application properties outside of your packaged jar
    (application-{profile}.properties and YAML variants).

  13. Profile-specific application properties packaged inside your jar
    (application-{profile}.properties and YAML variants).

  14. Application properties outside of your packaged jar (application.properties and YAML variants).
  15. Application properties packaged inside your jar
    (application.properties and YAML variants).
  16. @PropertySource
    annotations on your @Configuration classes.
  17. Default properties
    (specified using SpringApplication.setDefaultProperties).

配置随机值

my.secret=${random.value}
my.number=${random.int}
my.bignumber=${random.long}
my.uuid=${random.uuid}
my.number.less.than.ten=${random.int(10)}
my.number.in.range=${random.int[1024,65536]}

配置 SpringApplication

public static void main(String[] args) {SpringApplication app = new SpringApplication(MySpringConfiguration.class);app.setBannerMode(Banner.Mode.OFF);app.run(args);
}

配置文件

默认会从 application.properties 这个文件中读取配置

这个文件放在

  • A /config subdirectory of the current directory.
  • The current directory
  • A classpath /config package
  • The classpath root
    这几个位置都可以。

类型安全的 配置 ConfigurationProperties

@Value(“${property}”) 可能会造成类型的不安全, 所以提供了
ConfigurationProperties

@ConfigurationProperties("foo")
public class FooProperties {private boolean enabled;private InetAddress remoteAddress;private final Security security = new Security();public boolean isEnabled() { ... }public void setEnabled(boolean enabled) { ... }public InetAddress getRemoteAddress() { ... }public void setRemoteAddress(InetAddress remoteAddress) { ... }public Security getSecurity() { ... }public static class Security {private String username;private String password;private List<String> roles = new ArrayList<>(Collections.singleton("USER"));public String getUsername() { ... }public void setUsername(String username) { ... }public String getPassword() { ... }public void setPassword(String password) { ... }public List<String> getRoles() { ... }public void setRoles(List<String> roles) { ... }}
}

ConfigurationProperties 需要设置setter方法。

profile

spring.profiles.active=dev,hsqldb

这篇关于Spring-boot参考(三) 配置的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Java五子棋之坐标校正

上篇针对了Java项目中的解构思维,在这篇内容中我们不妨从整体项目中拆解拿出一个非常重要的五子棋逻辑实现:坐标校正,我们如何使漫无目的鼠标点击变得有序化和可控化呢? 目录 一、从鼠标监听到获取坐标 1.MouseListener和MouseAdapter 2.mousePressed方法 二、坐标校正的具体实现方法 1.关于fillOval方法 2.坐标获取 3.坐标转换 4.坐

Spring Cloud:构建分布式系统的利器

引言 在当今的云计算和微服务架构时代,构建高效、可靠的分布式系统成为软件开发的重要任务。Spring Cloud 提供了一套完整的解决方案,帮助开发者快速构建分布式系统中的一些常见模式(例如配置管理、服务发现、断路器等)。本文将探讨 Spring Cloud 的定义、核心组件、应用场景以及未来的发展趋势。 什么是 Spring Cloud Spring Cloud 是一个基于 Spring

Linux 安装、配置Tomcat 的HTTPS

Linux 安装 、配置Tomcat的HTTPS 安装Tomcat 这里选择的是 tomcat 10.X ,需要Java 11及更高版本 Binary Distributions ->Core->选择 tar.gz包 下载、上传到内网服务器 /opt 目录tar -xzf 解压将解压的根目录改名为 tomat-10 并移动到 /opt 下, 形成个人习惯的路径 /opt/tomcat-10

Javascript高级程序设计(第四版)--学习记录之变量、内存

原始值与引用值 原始值:简单的数据即基础数据类型,按值访问。 引用值:由多个值构成的对象即复杂数据类型,按引用访问。 动态属性 对于引用值而言,可以随时添加、修改和删除其属性和方法。 let person = new Object();person.name = 'Jason';person.age = 42;console.log(person.name,person.age);//'J

java8的新特性之一(Java Lambda表达式)

1:Java8的新特性 Lambda 表达式: 允许以更简洁的方式表示匿名函数(或称为闭包)。可以将Lambda表达式作为参数传递给方法或赋值给函数式接口类型的变量。 Stream API: 提供了一种处理集合数据的流式处理方式,支持函数式编程风格。 允许以声明性方式处理数据集合(如List、Set等)。提供了一系列操作,如map、filter、reduce等,以支持复杂的查询和转

uniapp接入微信小程序原生代码配置方案(优化版)

uniapp项目需要把微信小程序原生语法的功能代码嵌套过来,无需把原生代码转换为uniapp,可以配置拷贝的方式集成过来 1、拷贝代码包到src目录 2、vue.config.js中配置原生代码包直接拷贝到编译目录中 3、pages.json中配置分包目录,原生入口组件的路径 4、manifest.json中配置分包,使用原生组件 5、需要把原生代码包里的页面修改成组件的方

Java面试八股之怎么通过Java程序判断JVM是32位还是64位

怎么通过Java程序判断JVM是32位还是64位 可以通过Java程序内部检查系统属性来判断当前运行的JVM是32位还是64位。以下是一个简单的方法: public class JvmBitCheck {public static void main(String[] args) {String arch = System.getProperty("os.arch");String dataM

详细分析Springmvc中的@ModelAttribute基本知识(附Demo)

目录 前言1. 注解用法1.1 方法参数1.2 方法1.3 类 2. 注解场景2.1 表单参数2.2 AJAX请求2.3 文件上传 3. 实战4. 总结 前言 将请求参数绑定到模型对象上,或者在请求处理之前添加模型属性 可以在方法参数、方法或者类上使用 一般适用这几种场景: 表单处理:通过 @ModelAttribute 将表单数据绑定到模型对象上预处理逻辑:在请求处理之前

eclipse运行springboot项目,找不到主类

解决办法尝试了很多种,下载sts压缩包行不通。最后解决办法如图: help--->Eclipse Marketplace--->Popular--->找到Spring Tools 3---->Installed。

JAVA读取MongoDB中的二进制图片并显示在页面上

1:Jsp页面: <td><img src="${ctx}/mongoImg/show"></td> 2:xml配置: <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001