Spring Boot Загрузка нескольких файлов свойств в виде списка объектов свойств - PullRequest
0 голосов
/ 13 ноября 2018

Помимо application.properties, моему сервису также необходимо загружать свойства сайтов. Если быть более точным, в папке sites у меня есть вложенные файлы site.properties, это файлы, которые мне нужно загрузить при запуске приложения и поместить их в список (Список сайтов)

project structure

Используя механизмы весенней загрузки, такие как @PropertySource, можно ли искать файлы с одинаковыми именами, сопоставлять каждый из них с классом свойств сайта и помещать в список?

1 Ответ

0 голосов
/ 27 ноября 2018

Я не уверен насчет

, внесенного в список

, но если я правильно понял, вы можете

найтиДля файлов с одинаковыми именами сопоставьте каждый из них с классом свойств сайта

, используя префикс в файле свойств, как показано ниже:

Project Structure

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);
        }
    }

site.properties в ресурсах / com /пример / demo / sites / site1 / mobile /

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

site.properties в ресурсах / com / example / demo / sites / site1 / web /

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

site.properties вresources / com / example / demo / sites / site2 / mobile /

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

site.properties в ресурсах / com / example / demo / sites / site2 / web /

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

site.properties в ресурсах / com / example / demo / sites / site3 / mobile /

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

site.properties в ресурсах / com / example / demo / sites / site3 / web /

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

Результат: Properties

...