SpringBoot总结(五)——@PropertySource注解与@ImportResource注解

一、@PropertySource注解

我们已经知道,@ConfigurationProperties@Value都可以用于获取配置文件的属性值,但是我们发现,这两个注解在SpringBoot项目中都是获取默认配置文件的属性值——即application.yml或者application.properties配置文件的属性值的。
关于@ConfigurationProperties@Value简单使用可以参考上一篇:
SpringBoot总结(四)——@Value和@ConfigurationProperties的区别


下面将介绍,怎样来引用其它配置文件的属性值。这时候我们就可以使用@PropertySource注解。

School.java

package com.example.bean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Component
@PropertySource(value = {"classpath:school.properties"})
@ConfigurationProperties(prefix = "school")
    public class School {

        private String lastName;

        private String address;

        private Date birth;

        private Map<String,Object> maps;

        private List<Object> lists;

        private Student student;

        public String getLastName() {
            return lastName;
        }

        public void setLastName(String lastName) {
            this.lastName = lastName;
        }

        public String getAddress() {
            return address;
        }

        public void setAddress(String address) {
            this.address = address;
        }

        public Date getBirth() {
            return birth;
        }

        public void setBirth(Date birth) {
            this.birth = birth;
        }

        public Map<String, Object> getMaps() {
            return maps;
        }

        public void setMaps(Map<String, Object> maps) {
            this.maps = maps;
        }

        public List<Object> getLists() {
            return lists;
        }

        public void setLists(List<Object> lists) {
            this.lists = lists;
        }

        public Student getStudent() {
            return student;
        }

        public void setStudent(Student student) {
            this.student = student;
        }

        @Override
        public String toString() {
            return "School{" +
                    "lastName='" + lastName + '\'' +
                    ", address='" + address + '\'' +
                    ", birth=" + birth +
                    ", maps=" + maps +
                    ", lists=" + lists +
                    ", student=" + student +
                    '}';
        }
}

Student.java

package com.example.bean;
import org.springframework.stereotype.Component;
@Component
public class Student {
    private String name;

    private Integer age;
    private String score;

    public Student() {
    }

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

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

    public String getScore() {
        return score;
    }

    public void setScore(String score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score='" + score + '\'' +
                '}';
    }
}

新建school.properties配置文件

school.last-name=学校
school.address=江苏
school.birth=2020/1/1
school.maps.k1=v1
school.maps.k2=v2
school.lists=a,b,c,d
school.student.name=李四
school.student.age=19

编写测试类:

package com.example;
import com.example.bean.School;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class Springboot02ApplicationTests {
    @Autowired
    School school;
    @Test
   public void contextLoads() {
        System.out.println(school);
    }
    
}

测试结果:
在这里插入图片描述
注:

@Component
@PropertySource("classpath:school.properties")
@ConfigurationProperties(prefix = "school")
  • @PropertySource注解:来获取我们编写的school.properties文件。
  • @ConfigurationProperties注解:进行属性的映射,获取值。

二、@ImportResource注解

@ImportResource注解的作用

  • @ImportResource注解用于导入Spring的配置文件,让配置文件(如applicationContext.xml)里面的内容生效。
  • Spring Boot里面没有Spring的配置文件,我们自己编写的配置文件,也不能自动识别;让Spring的配置文件生效,加载进来。
    下面是用创建一个单独的配置类,全注解的方式来加载此XML bean定义文件的使用:
package com.example.config;

import com.example.service.HelloService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyConfig {

    @Bean
    public HelloService  helloService() {
        return new HelloService();
    }
}

  • @Configuration注解:指定当前的类是一个配置类。
  • @Bean注解:给容器中添加组件。将方法的返回值添加到容器中,容器中的默认id就是方法名。
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小Java开发者

“是一种鼓励,你懂的”

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值