• @PropertySource与@ImportResource的区别


    个人感悟:


    spring boot主配置文件为application.yml、application.yaml、application.xml 这三个扩散名一人比一个优先级高。即xml文件中配置同一属性的值,会覆盖前边属性的值。
    其实很多java类或接口,字段与这些主配置文件中属性是 绑定的。
    当我们自己创建类,也想放到容器中,可以单独建立文件,可以通过@PropertySource与@ImportResource这两个注解来注入到Servlet容器中。
    前者适合yml、yaml格式,而@importResource则适用于xml文件格式,如beans.xml 可以把其注解到窗口中,不过要放在spring boot主配置文件头部

    @PropertySource 自定义配置文件名称,用于spring boot 配置文件(yml或properties)与实体属性映射。
    引入说明

    在从配置文件里获取值,与JavaBean做映射。存在一个问题,我们是从主配置(application.yml)里读取的。如果全部的配置都写到application里,那么主配置就会显得特别臃肿。为了按照不同模块自定义不同的配置文件引入了@PropertySource
    配置
    主配置文件  application.yml

    person.properties
    person.lastName=李四
    person.age=25
    person.birth=2017/12/15
    person.boss=true
    person.maps.key1=value1
    person.maps.key2=value2
    person.lists=a,b,c
    person.dog.name=dog
    person.dog.age=2


    JavaBean --> Person.java

    @PropertySource(value = {"classpath:person.properties"})
    @ConfigurationProperties(prefix = "person")
    @Component
    public class Person {
        private String lastName;
        private Integer age;
        private boolean isBoss;
        private Date birth;
    
        private Map<String, Object> maps;
        private List<Object> lists;
        private Dog dog;
        ...setter/getter/toString...
    }



    这样一个注解(@PropertySource(value = {“classpath:person.properties”}))就可以搞定不在主配置里读取,按照不同的功能模块划分出不同的配置文件。
    @ImportResource

    一般情况下我们自定义的xml配置文件,默认情况下这个bean是不会加载到Spring容器中来的。需要@ImportResource注解将这个配置文件加载进来。
    配置

    <?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="helloService" class="com.chentongwei.springboot.service.HelloService"></bean>
    </beans>

    JavaBean

    public class HelloService {
    
    }

    修改启动类@SpringBootApplication

    @ImportResource(locations = {"classpath:beans.xml"})
    public class Springboot02ConfigApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(Springboot02ConfigApplication.class, args);
        }
    }



    小结
       @PropertySource
        @PropertySource 用于引入*.Properties或者 .yml 用于给javabean实体类注入值

    @Component
    @ConfigurationProperties(prefix="bird2")  //需要与@Component 配合使用,没有使用@PropertySource()则指向全局配置文件application.yml或application.properties
    //只能指向.properties文件,不可以指向.yml文件 没有此文件则指向主配置文件 @PropertySource(value
    ={"classpath:entity/pigean.properties"},ignoreResourceNotFound=false,encoding="UTF-8")


        @ImportResource 用于引入.xml 类型的配置文件 在spring boot中已经被配置类替代,且要求放在配置启动类上
      
        @ImportResource一般用于启动类上
    ————————————————
    版权声明:本文为CSDN博主「zzh_spring」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/zzh_spring/article/details/89331957

    做产品的程序,才是好的程序员!
  • 相关阅读:
    miniui mini-combobox的使用
    xsd文件记录
    Hibernate 一次查询分多次返回 避免内存溢出
    卡口扩展信息
    删除 maven仓库,更新失败的jar包命令
    杀windows进程
    layer 遮罩层等待
    math() 对象
    JavaScript 字符串方法
    JavaScript 数组遍历方法;
  • 原文地址:https://www.cnblogs.com/asplover/p/13299496.html
Copyright © 2020-2023  润新知