SpringBoot 配置(学习笔记8)

1、@PropertySource 和 @ImportResource区别

      1)、@PropertySource:加载指定配置文件


利用Spring的@PropertySource和@Value两个注解从配置文件properties中读取值。

@Component+@PropertySource+@Value==强大+便捷+高效


 2)、 @ImportResource:导入Spring的配置文件,让配置文件里面的内容生效。
               Spring Boot里面没有Spring的配置文件,我们自己编写的配置文件,也不能自动识别;
                想让Spring配置文件生效,加载进来;则需要@ImportResource标注在一个配置类上


ImportResource(locations = {"classpath:bean.xml"})
导入Spring配置文件,让其生效 

-----------------------------------------------------------------------------------------------------------------------------

上面Spring配置文件方式,在Spring Boot中不推荐使用,而SpringBoot推荐给容器中添加组件的方式,推荐使用全注解的方式。@Configuration和@Bean是Spring底层注解

1、配置类 类似于 Spring的配置文件

2、使用@Bean给容器中添加组件




--------------------------------------------------------------------------------------------------------------------

配置文件占位符:


server.port=80
#配置user属性值
user.uname=lisi-${random.uuid}
user.age=${random.int}
user.states=true
user.nickName=昵称${user.abc:无名}
user.is-num=0
user.birth=2017/03/11 23:08:19
user.maps.k1=v1
user.maps.k2=v2
user.lists=asdlf,123,true,测试,20.89
user.dog.name=${user.uname}的小狗
user.dog.age=1

感谢--尚硅谷


猜你喜欢

转载自blog.csdn.net/yufang131/article/details/80257084