springboot学习笔记(四)

1.@PropertySource:默认会加载application.properties/application.

yml文件中的数据。例如:修改application.properties为conf.

properties,则在实体类中加上@PropertySource(value={“classpath:

conf.properties”})。@PropertySource只能加载properties,不能加载yaml。

 

2.@ImportResource

      springBoot自动装配/自动配置,spring等配置文件,默认会被springboot自动给配置好。

如果要自己编写spring等配置文件,springboot能否识别?

答:默认不识别。如果需要识别,则需要在springboot的主配置类上通过@ImportResource指定配置文件的路径。但是不推荐手写spring配置文件。

配置:xml配置文件;通过注解配置文件。Springboot推荐使用注解方式来配置:写一个配置类    

@Configuration   

@Bean

示例:@Bean

//配置类   相当于配置文件,此类中全是注解等价于spring.xml

@Configuration

public class AppConfig {

@Bean

public StudentService stuService() {

StudentService stuService = new StudentService() ;

StudentDao stuDao = new StudentDao() ;

stuService.setStudentDao(stuDao) ;

return stuService ;  //返回值<bean class="xxxxxxxxx">  而id则就是方法名

}

}

 

3.springBoot全局配置文件中的占位符表达式:

常用占位符表达式:

①随机数:

${random.uuid}:uuid

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

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

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

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

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

②引用变量值:

yml中:student:

          name: ${student.user.name}

实际引用的是properties中的student.user.name=z167

 

yml中:student:

          name: ${student.user.name:无名}  //即student.user.name若不存在,则使用默认值:无名。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值