@ImportSource: 导入spring的配置文件,使其生效;
Springbooot中是 没有Spring配置文件的,我们自己编写的配置文件,也不会自动识别。
可以使用@ImportSource,让spring的配置类生效
注:@ImportSource 必须要标注在一个配置类上
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="userService" class="com.gm.service.UserService"></bean>
</beans>
如果说,我们项目中不允许出现.xml文件怎么办呢?
SpringBoot 推荐给容器中添加组件的方式;推荐使用全注解
@Configuration ------ spring配置文件
@Bean —
/**
* @Author Administrator
* @Date 2020/5/22 14:51
* @Configuration 标注在哪个类上,就代表当前是springboot的配置类
*
*<bean id="userService" class="com.gm.service.UserService"></bean>
**/
@Configuration
public class BootConfig {
// 把返回值加载到容器中
@Bean
public UserService userService(){
System.out.println("正在给spring容器注入组件");
return new UserService();
}
}
6.6 占位符
6.6 .1随机数
${random.value}、${random.int}、${random.long}
${random.int(10)}、${random.int[1024,65536]}
赋值 、 取值 、 自定义
user.user-name= 夏明翰${random.UUID}
user.age=${random.int}
user.gender=男
user.isgirl=true
user.maps.k1=v1
user.maps.k2=v2
user.sub=java,python,html
user.girl.name=${user.user-name}${user.password:的第二个}老Boby
user.girl.age=16
##7、Profiles
7.1多Profiles文件
我们在主配置文件编写的时候,文件名可以是 :
application.properties/.yml
application-{profiles}.properties/.yml
当项目中同时存在默认配置文件喝profiles文件时,
默认使用 application.properties/.yml
7.2 YAML支持多文档块
server:
port: 8084
spring:
profiles:
active: prov
---
server:
port: 8085
spring:
profiles: devs
---
server:
port: 8086
spring:
profiles: prov
7.3 激活制定的profiles的方法
1. 在配置文件中激活 spring.profiles.active: prov
- 在spring环境里去激活 --spring.profiles.active=devs
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9VMs6yKt-1590192565899)(C:\Users\Administrator\Desktop\springBoot笔记\images\2.PNG)]
-
命令行:
java -jar springboot-03-config2-0.0.1-SNAPSHOT.jar --spring.profiles.active=devs
-
虚拟机参数:
-Dspring.profiles.active=devs