Spring Boot 实例解析:配置文件

  1. SpringBoot 的热部署:
    1. Spring 为开发者提供了一个名为 spring-boot-devtools 的模块来使用 SpringBoot 应用支持热部署,提高开发者的效率,无需手动重启 SpringBoot 应用
    2. 引入依赖:
      <dependency> 
          <groupId>org.springframework.boot</groupId> 
          <artifactId>spring-boot-devtools</artifactId> 
          <optional>true</optional> 
      </dependency>
    3. 修改 Java 代码或者配置文件模板后可以通过 Ctrl + F9 来实现热部署
  2. 配置文件:
    1. SpringBoot 使用一个全局的配置文件,配置文件名是固定的
      1. application.properties
      2. application.yml
    2. 配置文件的作用:修改 SpringBoot 自动装配的默认值,SpringBoot 在底层都给自动配置好
    3. 标记语言:
      1. 以前的配置文件:大多都是 xx.xml 文件
      2. YAML:以数据为中心,比 json、xml 更适合做配置文件
      3. 例如:
  3. YAML 语法:
    1. 基本语法:
      1. K:(空格) V:表示一对键值对(空格必须有)
      2. 只要是左对齐的一列数据,都是同一个层级的
      3. 属性和值也是大小写敏感
    2. 值的写法:
      1. 字面量:普通的值(数字、字符串、布尔)
      2. K:V:字面量直接来写
      3. 字符串默认不用加上单引号或双引号
      4. " ":双引号,不会转义字符串里面的特殊字符,特殊字符会作为本身想表示的意思:
        1. 例如:name:"zhangsan \n lisi"  ==输出==> zhangsan 换行 lisi
      5. ' ':单引号,会转义特殊字符,特殊字符最终只是一个普通的字符串数据
        1. 例如:name:'zhangsan \n lisi' ==输出==> zhangsan \n lisi
  4. 对象、Map(属性和值):
    1. K:V:在下一行来写对象的属性和值的关系,注意缩进
    2. 对象:key:value 方式
    3. 数组:用 - 表示数组中的一个元素
  5. 配置文件注入:
    1. 配置文件:
      person:
        laseName: hello
        age: 18
        boss: false
        birth: 2019-09-09
        maps: {k1:v1,k2:v2}
        lists: 
          - lisi
          - zhaoliiu
        dog: 
          name: 小狗
          age: 12
    2. JavaBean:
      @Component
      @ConfigurationProperties(prefix = "person")
      public class Person {
          private String laseName;
          private Integer age;
          private Boolean boss;
          private Date birth;
          
          private Map<String, Object> maps;
          private List<Object> lists;
          private Dog dog;
      }
    3. 导入文件处理器:
      <!‐‐导入配置文件处理器,配置文件进行绑定就会有提示‐‐>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring‐boot‐configuration‐processor</artifactId>
          <optional>true</optional>
      </dependency>
    4. properties 配置文件在 IDEA 中默认 UTF-8 乱码问题:
    5. @Value 获取值和@ConfigurationProperties 获取值比较:
      1. 松散绑定:大小写区别,@ConfigurationProperties 会自动找到驼峰命名法的哪个字段 ==> @Value  注解不支持自动寻找
      2. SqEL:@Value 注解中可以使用 #{} 和 ${} 进行赋值,而 @ConfigurationProperties 不支持这样赋值
      3. JSR303 数据校验:例如 @Emile 注解
        1. 在 @ConfigurationProperties 中加入这个注解,在配置文件中相对应的形式应给为 Emile 形式,否则会报错
        2. 在 @Value 注解注入值使用这个数据校验注解是无效的
      4. 复杂数据封装:
        1. @Value 注解不支持除基本数据类型以外类型封装,而 @ConfigurationProperties 注解支持复杂数据封装
      5. 在某个业务中获取一个配置文件中的某个值,使用 @Value 注解
      6. 编写一个 JavaBean 来和配置文件进行映射,直接使用 @ConfigurationProperties 注解
    6. 配置文件:
      spring.application.name=S45SpringBootDemoApplicationTests
      
      person.email=kkkk
      person.hello=lucky
      person.last-name=张三
      person.birth=2017/2/4
      person.age=19
      person.boss=true
      person.list="dog,cat,animal"
      person.maps.key1=value1
      person.maps.key2=value2
      person.dog.name=${person.hello}_dog
      person.dog.age=12
    7. @Value 注解:注入值数据校验
      1. 注意:@Value 注解不能注入复杂属性(对象,集合等)
        @Conponent
        @Validated     //加入校验方式
        public class Person {
            //传入的值必须是邮箱格式,否则会报错
            @Email
            private String email;
        
            private String hello;
        
            //${} : 为取值和 application.properties 配置文件中字段一样的值
            @Value("${person.last-name}")
            private String lastName;
        
            //#{} : 可以写表达式
            @Value("#{3*2}")
            private Integer age;
        
            //@Value 注解也可以直接赋值
            @Value("true")
            private boolean boss;
        
            //@Value注解不能注入复杂属性
            private Date birth;
        
            private Map<String,String> maps;
        
            private List<String> list;
            //set  get 方法
            
            }
    8. @ConfigurationProperties 注解映射注入属性:
      1. 注意:容器中的组件才能使用该注解
        @Component
        //将本类中的所有属性和配置文件中 person2 下面的所有属性进行映射
        @ConfigurationProperties(prefix = "person2")
        @Validated    //校验注解
        public class Person2 {
        
            String email;
        
            String hello;
        
        
            String lastName;
        
            int age;
        
            boolean boss;
        
            Date birth;
        
            Map<String,String> maps;
        
            List<String> list;
        
            Dog dog;
        }
    9. 导入配置文件处理器:
      <!‐‐导入配置文件处理器,配置文件进行绑定就会有提示‐‐>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring‐boot‐configuration‐processor</artifactId>
          <optional>true</optional>
      </dependency>
  6. @PropertySource & @ImportResource & @Bean:
    1. @PropertySource:加载指定的配置文件
      //@PropertySource:加载指定配置文件
      @PropertySource(value={"classpath:person2.properties"})
      @Component
      //将本类中的所有属性和配置文件中 person2 下面的所有属性进行映射
      @ConfigurationProperties(prefix = "person2")
      public class Person2 {
      
          String email;
      
          String hello;
      
          String lastName;
      
          int age;
      
          boolean boss;
      
          Date birth;
      
          Map<String,String> maps;
      
          List<String> list;
      
          Dog dog;
      }
    2. @ImportResource:导入 Spring 的配置文件,让配置文件里面的内容生效
      1. SpringBoot 里面没有 Spring 的配置文件,我们自己编写的配置文件也不能自动识别
      2. 编写 applicationContext.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="personservice" class="com.example.springbootdemo.Service.impl.Person2ServiceImpl">
            </bean>
        </beans>
      3. 导入 Spring 的配置让其生效:
      4. 配置类注入测试:
        @RunWith(SpringRunner.class)
        @SpringBootTest
        public class TxDemo3ApplicationTests {
            //注入对象
            @Autowired
            ApplicationContext applicationContext;
        
            @Test
            public void contextLoads01() {
                System.out.println(applicationContext.getBean("personservice"));
            }
        }
    3. @Bean:给容器中添加组件
      1. 配置类 @Configuration  ==> 配置文件
      2. 使用 @Bean 给容器中添加组件
        //@Configuration:标明当前是一个配置类,代替之前的 Spring 配置文件
        @Configuration
        public class MyAppConfig {
        
            //将方法返回的值添加到容器当中,当前组件默认 ID 为当前文件名
            //@Bean注解给容器中添加组件
            @Bean
            public HelloService HelloService01(){
                System.out.println("配置类 @Bean 给容器中添加了组件");
                return new HelloServiceImpl();
            }
        }
本项目示例基于spring boot 最新版本(2.1.9)实现,Spring BootSpring Cloud 学习示例,将持续更新…… 在基于Spring BootSpring Cloud 分布微服务开发过程中,根据实际项目环境,需要选择、集成符合项目需求的各种组件和积累各种解决方案。基于这样的背景下,我开源了本示例项目,方便大家快速上手Spring BootSpring Cloud 。 每个示例都带有详细的介绍文档、作者在使用过程中踩过的坑、解决方案及参考资料,方便快速上手为你提供学习捷径,少绕弯路,提高开发效率。 有需要写关于spring bootspring cloud示例,可以给我提issue哦 ## 项目介绍 spring boot demo 是一个Spring BootSpring Cloud的项目示例,根据市场主流的后端技术,共集成了30+个demo,未来将持续更新。该项目包含helloworld(快速入门)、web(ssh项目快速搭建)、aop(切面编程)、data-redis(redis缓存)、quartz(集群任务实现)、shiro(权限管理)、oauth2(四种认证模式)、shign(接口参数防篡改重放)、encoder(用户密码设计)、actuator(服务监控)、cloud-config(配置中心)、cloud-gateway(服务网关)、email(邮件发送)、cloud-alibaba(微服务全家桶)等模块 ### 开发环境 - JDK1.8 + - Maven 3.5 + - IntelliJ IDEA ULTIMATE 2019.1 - MySql 5.7 + ### Spring Boot 模块 模块名称|主要内容 ---|--- helloworld|[spring mvc,Spring Boot项目创建,单元测试](https://github.com/smltq/spring-boot-demo/blob/master/helloworld/HELP.md) web|[ssh项目,spring mvc,过滤器,拦截器,监视器,thymeleaf,lombok,jquery,bootstrap,mysql](https://github.com/smltq/spring-boot-demo/blob/master/web/HELP.md) aop|[aop,正则,前置通知,后置通知,环绕通知](https://github.com/smltq/spring-boot-demo/blob/master/aop/HELP.md) data-redis|[lettuce,redis,session redis,YAML配置,连接池,对象存储](https://github.com/smltq/spring-boot-demo/blob/master/data-redis/HELP.md) quartz|[Spring Scheduler,Quartz,分布式调度,集群,mysql持久化等](https://github.com/smltq/spring-boot-demo/blob/master/quartz/HELP.md) shiro|[授权、认证、加解密、统一异常处理](https://github.com/smltq/spring-boot-demo/blob/master/shiro/HELP.md) sign|[防篡改、防重放、文档自动生成](https://github.com/smltq/spring-boot-demo/blob/master/sign/HELP.md) security|[授权、认证、加解密、mybatis plus使用](https://github.com/smltq/spring-boot-demo/blob/master/security/HELP.md) mybatis-plus-generator|[基于mybatisplus代码自动生成](https://github.com/smltq/spring-boot-demo/blob/master/mybatis-plus-generator) mybatis-plus-crud|[基于mybatisplus实现数据库增、册、改、查](https://github.com/smltq/spring-boot-demo/blob/master/mybatis-plus-crud) encoder|[主流加密算法介绍、用户加密算法推荐](https://github.com/smltq/spring-boot-demo/blob/master/encoder/HELP.md) actuator|[autuator介绍](https://github.com/smltq/spring-boot-demo/blob/master/actuator/README.md) admin|[可视化服务监控、使用](https://github.com/smltq/spring-boot-demo/blob/master/admin/README.md) security-oauth2-credentials|[oauth2实现密码模式、客户端模式](https://github.com/smltq/spring-boot-demo/blob/master/security-oauth2-credentials/README.md) security-oauth2-auth-code|[基于spring boot实现oauth2授权模式](https://github.com/smltq/spring-boot-demo/blob/master/security-oauth2-auth-code/README.md) mybatis-multi-datasource|[mybatis、数据库集群、读写分离、读库负载均衡](https://github.com/smltq/spring-boot-demo/blob/master/mybatis-multi-datasource) template-thymeleaf|[thymeleaf实现应用国际化示例](https://github.com/smltq/spring-boot-demo/blob/master/template-thymeleaf) mq-redis|[redis之mq实现,发布订阅模式](https://github.com/smltq/spring-boot-demo/blob/master/mq-redis) email|[email实现邮件发送](https://github.com/smltq/spring-boot-demo/blob/master/email) jGit|[java调用git命令、jgit使用等](https://github.com/smltq/spring-boot-demo/blob/master/jGit) webmagic|[webmagic实现某电影网站爬虫示例](https://github.com/smltq/spring-boot-demo/blob/master/webmagic) netty|[基于BIO、NIO等tcp服务器搭建介绍](https://github.com/smltq/spring-boot-demo/blob/master/netty) ### Spring Cloud 模块 模块名称|主要内容 ---|--- cloud-oauth2-auth-code|[基于spring cloud实现oath2授权模式](https://github.com/smltq/spring-boot-demo/blob/master/cloud-oauth2-auth-code) cloud-gateway|[API主流网关、gateway快速上手](https://github.com/smltq/spring-boot-demo/blob/master/cloud-gateway) cloud-config|[配置中心(服务端、客户端)示例](https://github.com/smltq/spring-boot-demo/blob/master/cloud-config) cloud-feign|[Eureka服务注册中心、负载均衡、声明式服务调用](https://github.com/smltq/spring-boot-demo/blob/master/cloud-feign) cloud-hystrix|[Hystrix服务容错、异常处理、注册中心示例](https://github.com/smltq/spring-boot-demo/blob/master/cloud-hystrix) cloud-zuul|[zuul服务网关、过滤器、路由转发、服务降级、负载均衡](https://github.com/smltq/spring-boot-demo/blob/master/cloud-zuul) cloud-alibaba|[nacos服务中心、配置中心、限流等使用(系列示例整理中...)](https://github.com/smltq/spring-boot-demo/blob/master/cloud-alibaba) #### Spring Cloud Alibaba 模块 模块名称|主要内容 ---|--- nacos|[Spring Cloud Alibaba(一)如何使用nacos服务注册和发现](https://github.com/smltq/spring-boot-demo/blob/master/cloud-alibaba/README1.md) config|[Spring Cloud Alibaba(二)配置中心多项目、多配置文件、分目录实现](https://github.com/smltq/spring-boot-demo/blob/master/cloud-alibaba/README2.md) Sentinel|[Spring Cloud Alibaba(三)Sentinel之熔断降级](https://github.com/smltq/spring-boot-demo/blob/master/cloud-alibaba/README3.md) Dubbo|[Spring Cloud Alibaba(四)Spring Cloud与Dubbo的融合](https://github.com/smltq/spring-boot-demo/blob/master/cloud-alibaba/README4.md) RocketMQ|[Spring Cloud Alibaba(五)RocketMQ 异步通信实现](https://github.com/smltq/spring-boot-demo/blob/master/cloud-alibaba/README5.md) ### 其它 模块名称|主要内容 ---|--- leetcode|[力扣题解目录](https://github.com/smltq/spring-boot-demo/blob/master/leetcode) ## Spring Boot 概述 Spring Boot简化了基于Spring的应用开发,通过少量的代码就能创建一个独立的、产品级别的Spring应用。 Spring BootSpring平台及第三方库提供开箱即用的设置,这样你就可以有条不紊地开始。多数Spring Boot应用只需要很少的Spring配置。 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Sprin
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值