学习日志:SpringBoot基础学习

ConfigurationProperties默认是从全局配置文件中获取值

@PropertySource:

加载指定的配置文件,也就是自家创建一个xxx.propertites,在实体类上家注解
@PropertySource(value={“classpath:xxx.propertites”})

@ImportResource:

导入Spring的配置文件,让配置文件的内容生效.因为我的自己创建编写的配置文件springboot不能自动识别,
如:

<?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.springexample.springboot01quick.Service.HelloService"/>
</beans>

要想被识别,必须在主程序类上加注解:

@ImportResource(locations = {"classpath:bean.xml"})

也可创建配置类:(springboot推荐)

profile

给springboot添加不同的环境支持,在开发中,又测试,开发等不同的环境。如在springboot项目中创建多个配置文件,如:
多profiles文件的方式

application-dev.xml开发使用
application-dov.xml测试使用
调试程序,系统默认使用aplication.properties,在配置文件中指定激活文件,spring.profiles.active=dev
YAML多文档方式
在YAML配置文件中,

server:
  port: 8080
spring:
  profiles:
    active: prod
    激活prod的profiles
---
server:
  port: 8081
spring:
  profiles: dev
---
server:
  port: 8082
spring:
  profiles: prod

命令行参数
在配置文件中指定

spring.profiles.active=dev

—分隔不同的文档块
项目打包后在命令行启动

java -jar xxx.jar --spring.profiles.active=dev;

虚拟机参数

-Dspring.profiles.active=dev

#配置文件加载位置

file: ./config/

    file: ./

        classpath: /config/

            classpath: / 

从高到低。
项目打包好以后,我们可以使用命令行参数的形式,启动项目的时候来指定配置文件的新位置;指定配置文件和默认加载的这些配置文件共同起作用形成互补配置;

java -jar xxx.jar --spring.config.location=/home/cloudlandboy/application.yaml
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值