springboot配置——@PropertySource,@ImportResource,配置文件的占位符

1.properties配置文件乱码问题

在设置中,搜File Encodings设置一下就OK了
在这里插入图片描述

2.使用@ConfigurationProperties(prefix = “person”)默认从全局配置文件中获取值,而 使用@PropertySource(value={})注解获取指定配置文件中的值 其中vlaue是数组,可以放多个配置文件 同时需要加上@ConfigurationProperties(prefix = “person”)

@PropertySource(value = {"classpath:person.properties"})
@Component
@ConfigurationProperties(prefix = "person")
public class Person {

    private String name;
    private int age;
    private String kind;
    private String email;

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getKind() {
        return kind;
    }

    public void setKind(String kind) {
        this.kind = kind;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", kind='" + kind + '\'' +
                ", email='" + email + '\'' +
                '}';
    }
}
person.properties配置文件:

person.name=小狗子
person.age=12
person.kind= 狗
person.email=123456@qq.com

3.使用注解@ImportResource(locations = {“classpath:beans.xml”})把spring配置文件中的bean注入容器中

@ImportResource加在主配置类上面

@ImportResource(locations = {"classpath:beans.xml"})
@SpringBootApplication
public class SpringbootDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootDemoApplication.class, args);
    }

}

在beans.xml中
  <bean id="helloService" class="com.fhxy.springboot_demo.service.HelloService"></bean>

创建一个com.fhxy.springboot_demo.service.HelloService类

在测试类中

    @Autowired
    ApplicationContext ioc;
    @Test
    public void testXml(){
        boolean b = ioc.containsBean("helloService");
        System.out.println(b);
    }
    输出为true就表明容器中存在这个bean

在springboot中不推荐使用这种方式,推荐使用全注解
使用@Configuration注解来标注配置类
使用@Bean标注方法, 将方法的返回值添加到容器,默认的id是方法名,可以用value来指定bean的id,可以指定多个id,通过value指定id后,默认的id将不起作用

//@Configuration标注一个配置类,代替spring配置文件
@Configuration
public class MyConfig {

    //@Bean 将方法的返回值添加到容器,默认的id是方法名,可以用value来指定bean的id,可以指定多个id,通过value指定id后,默认的id将不起作用
    @Bean(value = {"xiaobai","xiaohei","xiaohuang"})
    public HelloService helloService(){

        System.out.println("给容器中添加组件了·········");
        return new HelloService();
    }
}

4.配置文件的占位符

这是yaml配置文件
person:
  name: 狗子${random.uuid}	——》在狗子后面追加一串随机uuid
  age: 12
  kind: ${person.add:hello}_狗	——》在狗前面加上person的add值,如果add没有值或者没有add属性,就显示hello_狗
  email: 123456@qq.com_${person.age}	——》加了个_占位符,效果跟第一个没啥区别
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值