springboot核心技术之配置详解(二)

SpringBoot配置详解
本文深入解析SpringBoot中@PropertySource、@ImportResource等注解的使用,探讨bean实体注册配置方式,配置文件占位符功能,profile多环境支持,以及配置文件加载规则。

一、@PropertySource注解

       1.1、作用:引入自定义配置文件,作用的bean将从此配置文件中读取配置

       1.2、以配置详解(一)中的实例讲解:

           1、创建自定义配置文件test.properties

people.name= 李四
people.age= 54
people.isTeacher= true
people.maps.key1= mark
people.maps.key2= 琼恩
people.list= [lisi,zhangsan,mark]
people.dog.name= 金毛
people.dog.age= 5
people.last-name= lisi

            2、将application.yaml中的people下的属性值注释掉

            3、在people类前面加上@PropertySource标签声明自定义的配置文件

@Component
@PropertySource(value = "classpath:test.properties")
@ConfigurationProperties(prefix = "people")
public class People {

    private String name;

    private int age;

            4、测试结果

{"name":"李四","age":54,"maps":{"key1":"mark","key2":"琼恩"},"list":["[lisi","zhangsan","mark]"],"dog":{"name":"金毛","age":5},"lastName":"lisi","teacher":false}

       1.3、总结

           1、@PropertySource标签只能加载properties结尾的文件,可以同时加载多个配置文件;

           2、@PropertySource标签引入自定义配置文件会产生中文乱码问题,需要解决中文乱码问题;

           3、当配置属性相同时,application.properties/yaml的优先级大于自定义的properties文件,如果两个文件配置的属性不一样则application.properties/yaml和自定义配置文件中的配置形成互补配置。

二、@ImportResource注解

     2.1、作用:将自定义的spring XMl配置文件导入IOC容器

     2.2、测试

        1、helloService类

public class HelloService {

    public void play(){
        System.out.println("helloService。。。");
    }

}

        2、自定义test.xml


    <bean id="helloService" class="com.example.demo.service.HelloService">

    </bean>

        3、测试结果

  @Autowired
    ApplicationContext applicationContext;

    @Test
    public void test01(){
        System.out.println(applicationContext.containsBean("helloService"));
    }
2019-03-14 08:33:33.631  INFO 14024 --- [           main] c.e.demo.SpringbootyamlApplicationTests  : Started SpringbootyamlApplicationTests in 4.563 seconds (JVM running for 6.608)
false
2019-03-14 08:33:34.128  INFO 14024 --- [       Thread-4] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'

          ---- 打印结果显示false,证明helloService的bean在IOC容器中不存在。

         4、修改:在springboot启动类前面加上@ImportResource注解导入自定义的spring  XML配置文件

@SpringBootApplication
@ImportResource("classpath:test.xml")
@EnableConfigurationProperties(People.class)
public class SpringbootyamlApplication {

         5、再进行测试

2019-03-14 08:36:57.949  INFO 5840 --- [           main] c.e.demo.SpringbootyamlApplicationTests  : Started SpringbootyamlApplicationTests in 4.041 seconds (JVM running for 5.229)
true
2019-03-14 08:36:58.196  INFO 5840 --- [       Thread-4] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'

          ---- 打印结果显示true,证明helloService的bean在IOC容器中存在。

三、springboot推荐的bean实体注册配置:另外一种是标签注册

     3.1、使用条件:springboot前面不使用@ImportResource标签,使用@configuration配置类

     3.2、定义bean加载的实体类

@Configuration
public class BeanConfiguration {

    @Bean
    public HelloService helloService(){
        return new HelloService();
    }

}

      3.3、测试结果

2019-03-14 09:11:03.818  INFO 12776 --- [           main] c.e.demo.SpringbootyamlApplicationTests  : Started SpringbootyamlApplicationTests in 4.163 seconds (JVM running for 5.534)
true
2019-03-14 09:11:04.053  INFO 12776 --- [       Thread-4] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'

      3.4、@bean标签的作用:可以作用于方法和实体类,作用于方法时会把方法返回的对象实例注入进spring IOC容器中,bean实体的id默认是方法名,也可以在bean标签中自定义实体bean 的id

四、配置文件占位符${}

      4.1、配置文件占位符在properties文件和yaml文件中均能起作用

      4.2、占位符语法

         1、随机数

${random.uuid}//随机生成uuid的值
${random.int}//整型随机
${random.long}//长整型随机
${random.int(10)}//10以内随机
${random.int[1024,65536]}//1024到65536随机

        2、配置文件中设置:以前面的people实体为例说明

people.name= 张三${random.uuid}//自动生成uuid
people.age= ${random.int[2,80]}//以2,80的区间随机生成年龄值
people.isTeacher= true
people.maps.key1= mark
people.maps.key2= 小微
people.list= [lisi,zhangsan,mark]
people.dog.name= 金毛
people.dog.age= 5
people.last-name= lisi__${people.name}//将前面的people.name值获取并加在lisi__后面

        3、测试结果:

{"name":"张三e86c13a7-9ced-4076-9f33-d232e6b33331","age":21,"maps":{"key1":"mark","key2":"小微","k1":"obj1","k2":"obj2"},"list":["[lisi","zhangsan","mark]"],"dog":{"name":"金毛","age":5},"lastName":"lisi__张三08ae29b2-dc55-4738-bcc3-e64f25bcecaf","teacher":false}

       4、声明不存在的属性值(people.hello)

people.dog.name= 金毛${people.hello}

       5、测试结果

{"name":null,"age":0,"maps":null,"list":null,"dog":{"name":"金毛${people.hello}","age":0},"lastName":null,"teacher":false}

       6、修改:给people.hello加入默认值

people.dog.name= 金毛${people.hello:hello小金金}

       7、更改后的测试结果

{"name":null,"age":0,"maps":null,"list":null,"dog":{"name":"金毛hello小金金","age":0},"lastName":null,"teacher":false}

五、profile多环境支持:以服务端口为例

    5.1、properties文件

        1、application.properties文件

server.port=8081

        2、application-dev.properties自定义文件

server.port=8082

        3、application-prod.properties生产环境配置文件

server.port=8083

        4、测试:启动工程

2019-03-14 10:13:13.518  INFO 13940 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8081 (http) with context path ''

         控制台端口默认采用的是application.properties定义的端口号

      5、修改:声明多环境配置文件

            application.properties文件

server.port=8081
spring.profiles.active=dev

      6、新的测试结果

2019-03-14 10:19:14.416  INFO 15872 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8082 (http) with context path ''

            此时就变成了application-dev中设置的端口号8082

5.2、yaml文件

       1、yaml多文档分割线: ---

server:
  port: 8081
spring:
  profiles:
    active: prod

---
server:
  port: 8082
spring:
  profiles: dev

---
server:
  port: 8083
spring:
  profiles: prod

        2、Tomcat打印输出结果

2019-03-14 10:29:35.913  INFO 11520 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8083 (http) with context path ''

          端口号为8083,就是prod文档定义的端口号

六、springboot配置文件的加载

      6.1、优先级规则

              file../config/*.properties

              file../*.properties

              classpath../config/*.properties

              classpath../*.properties

             file是指springboot工程根目录,classpath则是resources文件夹,以上四种配置文件优先级由上到下依次递减,工程目录下的config文件夹里面的配置文件优先级最高,然后这四种配置文件配置内容会形成互补配置。

      6.2、测试:以server.port为例

         1、file../config/application.properties配置

server.port=8081

         2、file../application.properties配置

server.port=8082

         3、 classpath../config/application.properties配置

server.port=8083

         4、 classpath../application.properties配置

server.port=8084

         5、Tomcat打印结果

2019-03-14 10:39:42.164  INFO 15372 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8081 (http) with context path ''

          端口号为8081,说明优先打印了file../config/application.properties配置。

        6、注意如果配置文件在file工程目录下没有在classpath下面,我们要想访问资源路径需要在配置文件中声明工程名

server.port=8081
server.context-path=/boot

            然后游览器访问路径需要加上此工程名;localhost:8081/boot/people

       7、spring.config.location更改默认配置文件

           步骤:将工程打包,运行jar包并输入--spring.config.location命令行加载自定义的配置文件,该自定义配置文件会与springboot工程下默认的配置文件一起起作用;常见于运维人员进行服务器上工程的测试与部署。

七、外部配置加载顺序 :优先级由高到低

        1.命令行参数:所有的配置都可以在命令行上进行指定;多个配置用空格分开; –配置项=值


java -jar spring-boot-02-config-02-0.0.1-SNAPSHOT.jar 
--server.port=8087 --server.context-path=/abc

         2、jar包外部的application-{profile}.properties或application.yml(带spring.profile)配置文件 
         3、jar包内部的application-{profile}.properties或application.yml(带spring.profile)配置文件 
         4、jar包外部的application.properties或application.yml(不带spring.profile)配置文件 
         5、jar包内部的application.properties或application.yml(不带spring.profile)配置文件

     注意:由jar包外向jar包内进行寻找,优先加载待profile的,再加载不带profile的。

         

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值