SpringBoot(3)

        我们知道,当我们创建一个SpringBoot项目的时候,所有的运行配置都是自动完成,且都是进行了默认的配置值,当我们创建好SpringBoot项目的以后,可以直接运行的。

        如果我们现在要修改SpringBoot项目自动完成的,怎么办?

        我们可以在SpringBoot项目中的resources目录中的application.properties文件中进行默认配置的修改操作。

application.properties文件

        这个文件就是用来修改默认配置信息的配置文件。

名称:application.properties / application.yml

        application.properties / application.yml就是同一个配置文件,后缀名的不同,表示这个文件中内容的书写风格不同。

例如:通过在核心配置文件中修改数据库访问的用户名

        application.properties[键值对]

        spring.datasource.username=root

        application.yml[阶梯状]

        spring:

                datasource:

                        username:root

查找需要修改的默认配置信息的键名称:

        修改默认配置信息的配置文件spring-boot-autoconfigure:2.6.4.jar-->META-INF-->spring.factories--># Auto Configure-->找到自己需要的自动配置类-->@EnableConfigurationProperties({DataSourceProperties.class})-->DataSourceProperties.class
-->@ConfigurationProperties(prefix = "spring.datasource")
        在application.properties / application.yml文件中修改spring.datasource.url=自己的新数据值

当我们找到需要修改的默认配置信息的键名称以后,键名称对应的值的数据类型有多种情况?

        值不能随便写,得符合键名称对应成员变量的数据类型。

application.yml文件

        1.数字类型---数字值

        2.布尔---true/false

        3.字符串--不要引号---字符串数据值中有转义字符,希望转义字符运行,使用""(双引号) stustring: "zhangsan\nlisi"   \n---换行

                                           字符串数据值中有转义字符,希望转义字符不运行,使用''(单引号)

                                                stustring: "zhangsan\nlisi"   \n---\n

        4.对象类型

                1.对象名称下一行,缩进配置

                        person-bean------对象名变量名称

                                perid: 1111---对象中的成员变量

                                pername: person---对象中成员变量

                2.json对象

                        person-bean:{"perid":2222,"pername":"myname"}

        5.List集合类型

                1.集合名称的下一行,缩进配置,需要【-】前缀

                         stulist:

                                - javase

                                - javaee

                                - javame

                2.[数据值1, 数据值2,..........]

                        stulist: ["javase","javaee","javame"]

        6.Set集合类型

                1.集合名称的下一行,缩进配置,需要【-】前缀

                          stuset:

                                - javase

                                - javaee

                                - javame

                2.[数据值1, 数据值2,..........]

                         stuset: ["javase","javaee","javame"]

        7.Map集合类型

                配置1 变量名称:{"key":value1,"key:value2".......}

                配置1 变量名称:{"key":"value1","key":"value2".......}

                stumap: {"myname":zhangsan,"myage":23}

                stumap: {"myname":"zhangsan","myage":"23"}

        注意使用.yml配置文件的时候:1.注意缩进,2.注意":"后面要有一个空格

application.properties文件

        1.数字类型---数字值 student.stuint =1002

        2.布尔---true/false student.stuboolean = true

        3.字符串---不要引号

        student.stustring = zhangsan

        字符串数据值中有转义字符,希望转义字符运行,使用""(双引号)

        student.stustring = "zhangsan\nlisi"

        字符串数据值中有转义字符,希望转义字符不运行,使用''(单引号)

        student.stustring = 'zhangsan\nlisi'

        4.对象类型的配置

                student.对象名称.对象成员变量名称 = 成员变量数据值

                student.person-bean.perid = 1001

                student.person-bean.pername = myname

        5.List集合类型的配置

                student.集合名称 = 数据值1,数据值 2,数据值3........

                student.stulist=javase,javaee,javame 

        6.Set集合类型的配置

                student.集合名称 = 数据值1,数据值 2,数据值3........

                student.stuset=javase,javaee,javame

        7.Map集合类型的配置

                student.集合名称.key = value

                student.stumap.myname=testzhangsan

                student.stumap.myage=23

例如:

        application.yml文件

        导入这个依赖包

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
</dependency>
student:
  stuint: 1002
  studouble: 12.5
  stuboolean: true
  stustring: "zhangsan\nlisi"
#对象型配置
#  person-bean:
#      perid: 111
#      pername: zhangsan
  person-bean: {"perid":2222,"pername":"myperson"}
#list集合类型配置
#  stulist:
#    - javase
#    - javame
#    - javaee
  stulist: ["javaee","javase","javame"]
#set集合类型配置
#  stuset:
#    - zhangsan
#    - lisi
#    - wangwu
  stuset: ["zhangsan","lisi","wangwu"]
  #Map集合类型的配置
  stumap: {"myname":"zhangsan","myage":"23"}

测试 

package com.springboot.demo;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Map;

@SpringBootTest
class Springbootdemo3ApplicationTests {
    @Autowired
    private StudentBean studentBean;


    @Test
   public  void testStudent() {
        System.out.println("int---"+studentBean.getStuint());
        System.out.println("double---"+studentBean.getStudouble());
        System.out.println("string---"+studentBean.getStustring());
        System.out.println("boolean---"+studentBean.isStuboolean());
        System.out.println("person---"+studentBean.getPersonBean().getPername());
        System.out.println("---------------------------------------");
        for(String info:studentBean.getStulist()){
            System.out.println("list集合类型---"+info);
        }
        System.out.println("---------------------------------------");
        for(String info:studentBean.getStuset()){
            System.out.println("set集合类型---"+info);
        }
        System.out.println("---------------------------------------");
        for(Map.Entry<String,String> map:studentBean.getStumap().entrySet()){
            System.out.println("map集合类型---"+map.getKey()+"="+map.getValue());
        }
    }

}

  

application.properties文件

student.stuint=1002
student.studouble=12.5
student.stustring=zhangsan
student.person-bean.perid=2222
student.person-bean.pername=myname
student.stulist=javase,javaee,javame
student.stuset=javase,javaee,javame
student.stumap.myname=zhangsan
student.stumap.myage=23
package com.springboot.demo;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Map;

@SpringBootTest
class Springbootdemo3ApplicationTests {
    @Autowired
    private StudentBean studentBean;


    @Test
   public  void testStudent() {
        System.out.println("int---"+studentBean.getStuint());
        System.out.println("double---"+studentBean.getStudouble());
        System.out.println("string---"+studentBean.getStustring());
        System.out.println("boolean---"+studentBean.isStuboolean());
        System.out.println("person---"+studentBean.getPersonBean().getPername());
        System.out.println("---------------------------------------");
        for(String info:studentBean.getStulist()){
            System.out.println("list集合类型---"+info);
        }
        System.out.println("---------------------------------------");
        for(String info:studentBean.getStuset()){
            System.out.println("set集合类型---"+info);
        }
        System.out.println("---------------------------------------");
        for(Map.Entry<String,String> map:studentBean.getStumap().entrySet()){
            System.out.println("map集合类型---"+map.getKey()+"="+map.getValue());
        }
    }

}

 通过@Value将数据值与javabean类中的成员变量绑定

不能通过@Value注解为对象类型

package com.springboot.demo;

import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Set;

@Setter
@Getter
@Component
//@ConfigurationProperties(prefix = "student")
public class StudentBean {
    @Value("1001")
    private  int stuint;
    @Value("12.5")
    private  double studouble;
    @Value("true")
    private boolean stuboolean;
    @Value("zhangsan")
    private String  stustring;
    private PersonBean  personBean;
    @Value("zhangsan,lisi,wangwu")
    private List<String> stulist;
    @Value("zhangsan,lisi,wangwu")
    private Set<String> stuset;
    private Map<String,String> stumap;
}

        通过@PropertySource({"classpath:xxxx.properties","classpath:xxx.properties"})注解和@Value注解将.properties资源文件中的数据绑定到javabean类的成员变量

不能通过@Value注解为对象类型

#{表达式}与${表达式}

        @Value("#{}") Spring表达式语言(简称SpEL)可以进行数据运算/类型转换

        @Value("${XXXX}")单一的从配置文件中引用数据值,读取数据值,不会进行数据运算/类型转换

        @ImportResource(value = "classpath:userbean.xml")

package com.springboot.demo.bean;

import lombok.Getter;
import lombok.Setter;

@Setter
@Getter
public class UserBean {
    private  int  userid;
    private  String  username;
    private  boolean  usersex;
    private  double  userheight;
}

resources目录下userbean.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="userBean" class="com.springboot.demo.bean.UserBean">
        <property name="userid" value="1003"></property>
        <property name="username" value="wangwu"></property>
        <property name="usersex" value="true"></property>
        <property name="userheight" value="168.9"></property>
    </bean>
</beans>
package com.springboot.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

@SpringBootApplication
@ImportResource(value = "classpath:userbean.xml")
public class Springbootdemo4Application {

    public static void main(String[] args) {
        SpringApplication.run(Springbootdemo4Application.class, args);
    }

}

测试类:

package com.springboot.demo;

import com.springboot.demo.bean.UserBean;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class Springbootdemo4ApplicationTests {

    @Autowired
    private UserBean userBean;

    @Test
    void contextLoads() {
        System.out.println(userBean);
    }

}

Profiles

        1.Profiles文件就是用来配置在不同环境下的配置数据

        2.因为在不同的环境下配置文件中配置的运行环境的数据是不同的,所以我们就需要灵活的在不同的运行环境下切换成对应的运行环境的数据,此时我们将不同的运行环境庶几乎,配置到不同的配置文件中,通过主配置文件application.properties中的spring.profiles.active属性完成切换

        application-dev.properties[开发环境配置]

        server.port=8080

        application-prod.properties[生产环境配置]

        server.port=9090

        application-properties[主配置]

        spring.profiles.active=prod[指定使用生产环境配置]

        application.properties [主配置]

        spring.profiles.active=dev[指定开发环境配置]
      

        yml文件配置

        application-devyml.yml[开发环境配置]

        server:

                port:8080

        application-prodyml.yml[生产环境配置]

        server:

                port:9090

        application.yml[主配置]

        spring:

                profiles:

                        active:pordyml[指定使用生产环境配置]

        spring:

                profiles:

                        active:devyml[指定使用开发环境配置]

主配置文件加载位置

        springboot启动会扫描以下位置的application.properties或者application.yml文件作为Springboot的默认配置文件

        -- 项目根目录/config/

        -- 项目根目录/

        --resources/config/

        -- resources:/

        以上是按照优先级从高到低的顺序,所有位置的文件都会被加载,高优先级配置内容会覆盖低优先级配置内容。

        SpringBoot会从这四个位置全部加载主配置文件;互补配置

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

java -jar testspringboot02-0.0.1-SNAPSHOT.jar --spring.confifig.location=F:/application.properties

外部配置加载顺序

        SpringBoot支持多种外部配置方式

        1.命令行参数

        2.来自java:comp/env的JNDI属性

        3.Java系统属性(System.getProperties())

        4.操作系统环境变量

        5.RandomvaluePropertySource配置的random.*属性值

        6.jar包外部的application-{profile}.properties或application.yml(带spring.profile)配置文件

        7.jar包内部的application-{profile}.properties或application.yml(带spring.profile)配置文件

        8.jar包外部的application.properties或application.yml(不带spring.profile)配置文件

        9.jar包内部的application.properties或application.yml(不带spring.profile)配置文件

        有限加载带profifile,在加载不带profifile,,由jar包外向jar包内进行寻找

        10.@Configuration注解类上的@PropertySource

        11.通过SpringApplication.setDefaultProperties指定的默认属性

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值