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若不存在,则使用默认值:无名。