【问题标题】:Spring Boot Load multiple properties files as a list of property objectsSpring Boot 加载多个属性文件作为属性对象列表
【发布时间】:2019-04-16 07:21:43
【问题描述】:

除了 application.properties,我的服务还需要加载站点属性。更准确地说,在 sites 文件夹中,我嵌套了 site.properties 文件,这些文件是我需要在应用程序启动时加载并将它们放入列表(列出站点)的文件

使用@PropertySource 之类的spring boot 机制,是否可以搜索到同名文件,将每个文件映射到Site 属性类并放入列表中?

【问题讨论】:

    标签: spring-boot properties-file


    【解决方案1】:

    我不确定

    加入列表

    但如果我理解正确,你可以

    搜索具有相同名称的文件,将每个文件映射到站点 属性类

    通过在属性文件中使用前缀如下示例:

    SitesAnnotation.java

    package com.example.demo;
    
    import org.springframework.beans.factory.annotation.Qualifier;
    
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    public class SitesAnnotation {
        @Target({
                ElementType.METHOD,
                ElementType.FIELD,
                ElementType.TYPE,
                ElementType.TYPE_PARAMETER
        })
        @Retention(RetentionPolicy.RUNTIME)
        @Qualifier
        public @interface Site1 {
        }
    
        @Target({
                ElementType.METHOD,
                ElementType.FIELD,
                ElementType.TYPE,
                ElementType.TYPE_PARAMETER
        })
        @Retention(RetentionPolicy.RUNTIME)
        @Qualifier
        public @interface Mobile1 {
        }
    
        @Target({
                ElementType.METHOD,
                ElementType.FIELD,
                ElementType.TYPE,
                ElementType.TYPE_PARAMETER
        })
        @Retention(RetentionPolicy.RUNTIME)
        @Qualifier
        public @interface Web1 {
        }
    
        @Target({
                ElementType.METHOD,
                ElementType.FIELD,
                ElementType.TYPE,
                ElementType.TYPE_PARAMETER
        })
        @Retention(RetentionPolicy.RUNTIME)
        @Qualifier
        public @interface Site2 {
        }
    
        @Target({
                ElementType.METHOD,
                ElementType.FIELD,
                ElementType.TYPE,
                ElementType.TYPE_PARAMETER
        })
        @Retention(RetentionPolicy.RUNTIME)
        @Qualifier
        public @interface Mobile2 {
        }
    
        @Target({
                ElementType.METHOD,
                ElementType.FIELD,
                ElementType.TYPE,
                ElementType.TYPE_PARAMETER
        })
        @Retention(RetentionPolicy.RUNTIME)
        @Qualifier
        public @interface Web2 {
        }
    
        @Target({
                ElementType.METHOD,
                ElementType.FIELD,
                ElementType.TYPE,
                ElementType.TYPE_PARAMETER
        })
        @Retention(RetentionPolicy.RUNTIME)
        @Qualifier
        public @interface Site3 {
        }
    
        @Target({
                ElementType.METHOD,
                ElementType.FIELD,
                ElementType.TYPE,
                ElementType.TYPE_PARAMETER
        })
        @Retention(RetentionPolicy.RUNTIME)
        @Qualifier
        public @interface Mobile3 {
        }
    
        @Target({
                ElementType.METHOD,
                ElementType.FIELD,
                ElementType.TYPE,
                ElementType.TYPE_PARAMETER
        })
        @Retention(RetentionPolicy.RUNTIME)
        @Qualifier
        public @interface Web3 {
        }
    }
    

    SitesConfig.java

    package com.example.demo;
    
    import lombok.Data;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.PropertySource;
    
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    import java.util.List;
    import java.util.Map;
    
    @Configuration
    @PropertySource("classpath:com/example/demo/sites/site1/mobile/site.properties")
    @PropertySource("classpath:com/example/demo/sites/site1/web/site.properties")
    @PropertySource("classpath:com/example/demo/sites/site2/mobile/site.properties")
    @PropertySource("classpath:com/example/demo/sites/site2/web/site.properties")
    @PropertySource("classpath:com/example/demo/sites/site3/mobile/site.properties")
    @PropertySource("classpath:com/example/demo/sites/site3/web/site.properties")
    public class SitesConfig {
    
        @Data                          // <<<<<<<< Lombok
        public static class Config {
            private String userName;
            private String passWord;
        }
    
        @Bean
        @SitesAnnotation.Site1
        @SitesAnnotation.Mobile1
        @ConfigurationProperties(prefix = "site1.mobile")
        public Config site1_mobile() {
            return new Config();
        }
    
        @Bean
        @SitesAnnotation.Site1
        @SitesAnnotation.Web1
        @ConfigurationProperties(prefix = "site1.web")
        public Config site1_web() {
            return new Config();
        }
    
        @Bean
        @SitesAnnotation.Site2
        @SitesAnnotation.Mobile2
        @ConfigurationProperties(prefix = "site2.mobile")
        public Config site2_mobile() {
            return new Config();
        }
    
        @Bean
        @SitesAnnotation.Site2
        @SitesAnnotation.Web2
        @ConfigurationProperties(prefix = "site2.web")
        public Config site2_web() {
            return new Config();
        }
    
    
        @Bean
        @SitesAnnotation.Site3
        @SitesAnnotation.Mobile3
        @ConfigurationProperties(prefix = "site3.mobile")
        public Config site3_mobile() {
            return new Config();
        }
    
        @Bean
        @SitesAnnotation.Site3
        @SitesAnnotation.Web3
        @ConfigurationProperties(prefix = "site3.web")
        public Config site3_web() {
            return new Config();
        }
    
    }
    

    DemoApplication.java

        package com.example.demo;
    
        import com.example.demo.Config.Controller;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.boot.SpringApplication;
        import org.springframework.boot.autoconfigure.SpringBootApplication;
        import org.springframework.context.ConfigurableApplicationContext;
    
        @SpringBootApplication
        public class DemoApplication {
            @Autowired
            @SitesAnnotation.Site1
            @SitesAnnotation.Mobile1
            SitesConfig.Config site1_mobile;
    
            @Autowired
            @SitesAnnotation.Site1
            @SitesAnnotation.Web1
            SitesConfig.Config site1_web;
    
            @Autowired
            @SitesAnnotation.Site2
            @SitesAnnotation.Mobile2
            SitesConfig.Config site2_mobile;
    
            @Autowired
            @SitesAnnotation.Site2
            @SitesAnnotation.Web2
            SitesConfig.Config site2_web;
    
    
            @Autowired
            @SitesAnnotation.Site3
            @SitesAnnotation.Mobile3
            SitesConfig.Config site3_mobile;
    
            @Autowired
            @SitesAnnotation.Site3
            @SitesAnnotation.Web3
            SitesConfig.Config site3_web;
    
            public static void main(String[] args) {
                try (ConfigurableApplicationContext ctx = SpringApplication.run(DemoApplication.class, args)) {
                    DemoApplication app = ctx.getBean(DemoApplication.class);
                    app.run(args);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
    
            public void run(String... args) throws Exception {
                System.out.println(site1_mobile);
                System.out.println(site1_web);
    
                System.out.println(site2_mobile);
                System.out.println(site2_web);
    
                System.out.println(site3_mobile);
                System.out.println(site3_web);
            }
        }
    

    资源/com/example/demo/sites/site1/mobile/中的site.properties

    site1.mobile.userName = site 1 - mobile - userName
    site1.mobile.passWord = site 1 - mobile - passWord
    

    资源/com/example/demo/sites/site1/web/中的site.properties

    site1.web.userName = site 1 - web- userName
    site1.web.passWord = site 1 - web - passWord
    

    资源/com/example/demo/sites/site2/mobile/中的site.properties

    site2.mobile.userName = site 2 - mobile - userName
    site2.mobile.passWord = site 2 - mobile - passWord
    

    资源/com/example/demo/sites/site2/web/中的site.properties

    site2.web.userName = site 2 - web- userName
    site2.web.passWord = site 2 - web - passWord
    

    资源/com/example/demo/sites/site3/mobile/中的site.properties

    site3.mobile.userName = site 3 - mobile - userName
    site3.mobile.passWord = site 3 - mobile - passWord
    

    资源/com/example/demo/sites/site3/web/中的site.properties

    site3.web.userName = site 3 - web- userName
    site3.web.passWord = site 3 - web - passWord
    

    结果:

    【讨论】:

      猜你喜欢
      • 2019-03-30
      • 2017-11-17
      • 2018-03-28
      • 1970-01-01
      • 2014-12-21
      • 2017-08-18
      • 2019-10-18
      • 2016-06-18
      • 1970-01-01
      相关资源
      最近更新 更多