springboot入门(3)

@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

  1. 在spring环境里去激活 --spring.profiles.active=devs
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9VMs6yKt-1590192565899)(C:\Users\Administrator\Desktop\springBoot笔记\images\2.PNG)]
  1. 命令行:

    ​ java -jar springboot-03-config2-0.0.1-SNAPSHOT.jar --spring.profiles.active=devs

  2. 虚拟机参数:

    -Dspring.profiles.active=devs

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值