spring boot @ImportResource和配置类

@PropertySource:默认会加载application.properties/application.yml文件中的数据:

    例如@PropertySource(value={"classpath:conf.properties"})

    加载conf.properties

    但是@PropertySource只能加载properties,不能加载yml

@ImportResource

    spring boot自动装配/自动配置, spring等配置文件默认被spring boot自动配置好。

    如果自己编写的spring配置文件,springboot默认不识别。如果想让spring boot识别,需要使用@ImportResource:

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

但是不推荐手写spring配置文件。

配置:xml配置文件,通过注解配置

扫描二维码关注公众号,回复: 6098315 查看本文章

spring boot 推荐使用注解方式进行配置:@Configuration  @Bean :

@Configuration//声明为配置类

public class AppConfig{

     @Bean//声明一个BEAN

    public XXXService xXXservice(){//<bean id="xXXservice" ,方法名即bean id

         XXXService service=new XXXService();

         XXXDao dao=new XXXDao();

         service.setDao(dao);//属性注入

          return service;

     }

}

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

a、随机数

    ${random.uuid} : uuid

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

    ${random.int} : 随机整数

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

    ${random.int(10) : 10以内的随机整数

    ${random.int[1024,65535]} : 指定范围内的随机整数

b、引用变量值

    yml中:    

people: 
    name: feifei
    age: 19
    pet: 
        name: ${people.name}.zuzu
        type: dog

yml也可引用properties中的属性值

name: ${people.name: somebody}.zuzu

表示如果 不存在 people.name 则将 somebody做为默认值返回.

    

猜你喜欢

转载自blog.csdn.net/zyfzhangyafei/article/details/89764284