zoukankan      html  css  js  c++  java
  • 5springboot的@ImportResource注解/配置类/占位符表达式

    @ImportResource注解

    springboot是自动装配/自动配置的,spring等配置文件 默认会被spring boot自动装配好。如果自己编写spring等配置文件,springboot默认是不识别自己编写的配置文件。

    如果想要被识别到,则需要在springboot主配置类上 通过@ImportResource注解 指定配置文件的路径进行识别。

    但是不推荐手写spring配置文件,对于配置文件,一般有两种配置方式,如下:

    配置方式:

    1. xml配置文件。
    2. 注解配置。

    注解配置主要应用在配置类,配置类 等价于 spring的配置文件,接下来展示配置类和spring配置文件之间的过程,看看两者之间的异曲同工之妙

    spring配置文件方式

    image-20200821211341420

    上图的spring配置文件很熟悉吧,spring配置文件配置好后,就可以直接在ioc容器中直接拿这个bean,接下来拿一下该bean,如下图:

    image-20200821201410860

    可以发现是拿不到bean,因为spring boot默认不会识别自己编写的spring配置文件,需要在spring boot的主配置类上通过@ImportResource注解指定spring配置文件的路径,如下图:

    image-20200821203039960

    可以发现test()方法可以拿到自己配置的spring配置文件里面的bean了,代表spring boot已经识别到了手写的spring配置文件,接下来展示 注解类的配置方式。

    注解类配置方式

    springboot 推荐使用注解方式 进行配置, 注解方式主要是通过注解类来实现,添加 @Configuration @Bean 注解就可以实现注解方式配置,过程如下:

    1. 首先创建配置类,注解方式的两个注解都已经加上了,注意:StudentService类记得带上setStudentDao方法,还有@autoWire导入的StudentDao对象,无论是spring配置文件还是配置类,如果要注入属性都要提供set方法。
    import com.example.Dao.StudentDao;
    import com.example.service.StudentService;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    /**
     * @author HainChen
     * @Description: 配置类
     * @createDate 2020-08-21 20:41
     * @Modified By:
     */
    
    @Configuration  //添加该注解代表,当前类是配置类
    public class AppConfig {
    
        /**
         * 一个方法代表一个bean,和spring配置文件那边配置bean一样,
         * spring配置文件的bean的id对应着这边的方法名,也就是 id = 方法名
         * setStudentDao(stuDao)对应着那边bean里面的property属性注入
         * @return 这里的返回值就对应那边的bean里面的class
         */
        @Bean
         public StudentService stuService(){
            StudentService stuService = new StudentService();
    
            StudentDao stuDao = new StudentDao();
            stuService.setStudentDao(stuDao);
            return stuService;
        }
    }
    
    1. 再次测试,看是否可以拿到配置类的bean

      image-20200821205741814

    可以发现注解方式配置类可以拿到bean,补充:dao代码是空的,service里面就只有一个setDao方法和@autoWire导入的Dao对象,只是用来演示,所以就不详细补充其他内容了。

    总结

    配置方式总共两种,一种spring配置文件,另一种是注解配置类。两者之间没有太大区别,只是形式不同,一个是xml文件,一个是class文件。配置类的方法名对应spring配置文件的bean里面的id返回值对应bean里面的class属性set方法对应bean里面的property属性注入,而且spring的property属性注入,本来Service里面还要提供一个set方法,才能完成注入。

    spring boot全局配置文件中的 占位符表达式

    1. 随机数 ${random.uuid}等

      ${random.uuid} : uuid

      ${random.value} : 随机字符串

      ${random.int} : 随机整型数

      ${random.long} : 随机长整型数

      ${random.int(10)} : 10以内的整型数

      ${random.int[1024,65536]} : 指定随机数范围

    2. 引用变量值

      1. properties被yml引用

        image-20200822104412809

      2. yml引用properties属性时,如果引用不了,可以设置默认值

        image-20200822104602981

  • 相关阅读:
    C语言I博客作业11
    第十四周助教总结
    C语言I博客作业10
    Tensorflow--MNIST简单全连接层分类
    记一些好看的Android开源菜单
    AS更新到3.5.2遇到ERROR: SSL peer shut down incorrectly问题
    Tensorflow tf.app.flags 的使用
    Android Sensor(传感器)
    《第一行代码》百分比布局出现的问题
    当Turtle遇见柯南?
  • 原文地址:https://www.cnblogs.com/unlasting/p/13544775.html
Copyright © 2011-2022 走看看