在使用spring boot过程中,可以发现项目中只需要极少的配置就能完成相应的功能,这归功于spring boot中的模块化配置,在pom.xml中依赖的每个Starter都有默认配置,而这些默认配置足以满足正常的功能开发。
如果需要修改自定义修改默认配置,spring boot 提供了很简便的方法,只需要在application.properties 中添加修改相应的配置。(spring boot启动的时候会读取application.properties这份默认配置)
一、修改默认配置
例1、spring boot 开发web应用的时候,默认tomcat的启动端口为8080,如果需要修改默认的端口,则需要在application.properties 添加以下记录:
server.port=8888
重启项目,启动日志可以看到:Tomcat started on port(s): 8888 (http) 启动端口为8888,浏览器中访问 http://localhost:8888 能正常访问。
例2、spring boot 开发中的数据库连接信息配置(这里使用com.alibaba 的 druid), 在application.properties 添加以下记录:
druid.url=jdbc:mysql://192.168.0.20:3306/test druid.driver-class=com.mysql.jdbc.Driver druid.username=root druid.password=123456 druid.initial-size=1 druid.min-idle=1 druid.max-active=20 druid.test-on-borrow=true
以上两个例子,说明了如需修改starter模块中的默认配置,只需要在在application.properties 添加需要修改的配置即可。
二、自定义属性配置
在application.properties中除了可以修改默认配置,我们还可以在这配置自定义的属性,并在实体bean中加载出来。
1、在application.properties中添加自定义属性配置
com.sam.name=sam com.sam.age=11 com.sam.desc=magical sam
2、编写Bean类,加载属性
Sam类需要添加@Component注解,让spring在启动的时候扫描到该类,并添加到spring容器中。
第一种:使用spring支持的@Value()加载
package com.sam.demo.conf; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; /** * @author sam * @since 2017/7/15 */ @Component public class Sam { //获取application.properties的属性 @Value("${com.sam.name}") private String name; @Value("${com.sam.age}") private int age; @Value("${com.sam.desc}") private String desc; //getter & setter }
第二种:使用@ConfigurationProperties(prefix="") 设置前缀,属性上不需要添加注解。
package com.sam.demo.conf; import org.springframework.stereotype.Component; /** * @author sam * @since 2017/7/15 */ @Component @ConfigurationProperties(prefix = "com.sam") public class Sam { private String name; private int age; private String desc; //getter & setter }
3、在controller中注入并使用Sam这个Bean。
package com.sam.demo.controller; import com.sam.demo.conf.Sam; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @author sam * @since 2017/7/14 */ @RestController public class IndexController { @Autowired private Sam sam; @RequestMapping("/index") public String index() { System.out.println(sam.getName() + " " + sam.getAge() + " " + sam.getDesc()); return "index"; } }
浏览器访问:http://localhost:8080/index ,控制台正常打印出sam的内容。
三、application.properties 属性配置详解
1、参数引用与random随机数方法的使用
在application.properties内可以直接通过${}引用其他属性的值,如下:
com.sam.name=sam com.sam.age=11 com.sam.desc=${name} is ${age} years old.
在application.properties中如果需要获取随机数,可以通过${random},如下:
#获取随机字符串 com.sam.randomValue=${random.value} #获取随机字符串:${random.value} #获取随机int:${random.int} #获取10以内的随机数:${random.int(10)} #获取10-20的随机数:${random.int[10,20]} #获取随机long:${random.long} #获取随机uuid:${random.uuid}
2、多环境配置
实际开发中可能会有不同的环境,有开发环境、测试环境、生成环境。对于每个环境相关配置都可能有所不同,如:数据库信息、端口配置、本地路径配置等。
如果每次切换不同环境都需要修改application.properties,那么操作是十分繁琐的。在spring boot中提供了多环境配置,使得我们切换环境变得简便。
在application.properties同目录下新建一下三个文件:
application-dev.properties //开发环境的配置文件 application-test.properties //测试环境的配置文件 application-prod.properties //生产环境的配置文件
上面三个文件分别对应了 开发、测试、生产 的配置内容,接下来就是应该怎么选择性引用这些配置了。
在application.properties添加:
spring.profiles.active=dev #引用测试的配置文件 #spring.profiles.active=test #引用生产的配置文件 #spring.profiles.active=prod
添加spring.profiles.active=dev后启动应用,会发现引用了dev的这份配置信息。
可以看出上面三个配置文件符合 application-{profile}.properties 格式,而在application.properties添加的 spring.profiles.active=dev 中的dev正是上面配置文件中的 profile。根据具体环境进行切换即刻。
用命令运行jar包启动应用的时候,可以指定相应的配置.
java -jar demo-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev
附:配置方式和优先级
这些方式优先级如下: a. 命令行参数 b. 来自java:comp/env的JNDI属性 c. Java系统属性(System.getProperties()) d. 操作系统环境变量 e. RandomValuePropertySource配置的random.*属性值 f. jar外部的application-{profile}.properties或application.yml(带spring.profile)配置文件 g. jar内部的application-{profile}.properties或application.yml(带spring.profile)配置文件 h. jar外部的application.properties或application.yml(不带spring.profile)配置文件 i. jar内部的application.properties或application.yml(不带spring.profile)配置文件 j. @Configuration注解类上的@PropertySource k. 通过SpringApplication.setDefaultProperties指定的默认属性
注:命令行参数这种jar包指定参数启动应用的方式,可能是不安全的,我们可以设置禁止这种方式启动应用,如下:
springApplication.setAddCommandLineProperties(false);
package com.sam.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { // SpringApplication.run(DemoApplication.class, args); SpringApplication springApplication = new SpringApplication(DemoApplication.class); //禁止命令行设置参数 springApplication.setAddCommandLineProperties(false); springApplication.run(args); } }
补充:
在spring boot 中配置除了支持 application.properties,还支持application.yml的配置方式,如下:
新建application.yml代替application.properties
server: port: 9999 com: sam: name: sam age: 11 desc: magical sam
注意:port: 9999 中间是有空格的,yml语法请参考:yml配置文件用法
以上是Spring Boot系列之属性配置以及自定义属性配置的具体介绍的详细内容。更多信息请关注PHP中文网其他相关文章!

java实现定时任务Jdk自带的库中,有两种方式可以实现定时任务,一种是Timer,另一种是ScheduledThreadPoolExecutor。Timer+TimerTask创建一个Timer就创建了一个线程,可以用来调度TimerTask任务Timer有四个构造方法,可以指定Timer线程的名字以及是否设置为为守护线程。默认名字Timer-编号,默认不是守护线程。主要有三个比较重要的方法:cancel():终止任务调度,取消当前调度的所有任务,正在运行的任务不受影响purge():从任务队

一、@RequestParam注解对应的axios传参方法以下面的这段Springjava代码为例,接口使用POST协议,需要接受的参数分别是tsCode、indexCols、table。针对这个Spring的HTTP接口,axios该如何传参?有几种方法?我们来一一介绍。@PostMapping("/line")publicList

SpringBoot和SpringCloud都是SpringFramework的扩展,它们可以帮助开发人员更快地构建和部署微服务应用程序,但它们各自有不同的用途和功能。SpringBoot是一个快速构建Java应用的框架,使得开发人员可以更快地创建和部署基于Spring的应用程序。它提供了一个简单、易于理解的方式来构建独立的、可执行的Spring应用

随着技术的更新迭代,Java5.0开始支持注解。而作为java中的领军框架spring,自从更新了2.5版本之后也开始慢慢舍弃xml配置,更多使用注解来控制spring框架。

作为一名Java开发者,学习和使用Spring框架已经是一项必不可少的技能。而随着云计算和微服务的盛行,学习和使用SpringCloud成为了另一个必须要掌握的技能。SpringCloud是一个基于SpringBoot的用于快速构建分布式系统的开发工具集。它为开发者提供了一系列的组件,包括服务注册与发现、配置中心、负载均衡和断路器等,使得开发者在构建微

1.Spring项目的创建1.1创建Maven项目第一步,创建Maven项目,Spring也是基于Maven的。1.2添加spring依赖第二步,在Maven项目中添加Spring的支持(spring-context,spring-beans)在pom.xml文件添加依赖项。org.springframeworkspring-context5.2.3.RELEASEorg.springframeworkspring-beans5.2.3.RELEASE刷新等待加载完成。1.3创建启动类第三步,创

spring设计模式有:1、依赖注入和控制反转;2、工厂模式;3、模板模式;4、观察者模式;5、装饰者模式;6、单例模式;7、策略模式和适配器模式等。详细介绍:1、依赖注入和控制反转: 这两个设计模式是Spring框架的核心。通过依赖注入,Spring负责管理和注入组件之间的依赖关系,降低了组件之间的耦合度。控制反转则是指将对象的创建和依赖关系的管理交给Spring容器等等。

SpringBean的生命周期管理一、SpringBean的生命周期通过以下方式来指定Bean的初始化和销毁方法,当Bean为单例时,Bean归Spring容器管理,Spring容器关闭,就会调用Bean的销毁方法当Bean为多例时,Bean不归Spring容器管理,Spring容器关闭,不会调用Bean的销毁方法二、通过@Bean的参数(initMethod,destroyMethod)指定Bean的初始化和销毁方法1、项目结构2、PersonpublicclassPerson{publicP


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

Atom编辑器mac版下载
最流行的的开源编辑器

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)