Spring Boot Load multiple properties files as a list of property objects
Apart from the application.properties, my service also needs to load sites properties. To be more precise, in the sites folder I have nested site.properties files, those are files that I need to load when the application is starting and put them to the list (List sites)
Using spring boot mechanisms like @PropertySource, is it possible to search for the files with the same name, map each of them to the Site property class and put to the list?
spring-boot properties-file
add a comment |
Apart from the application.properties, my service also needs to load sites properties. To be more precise, in the sites folder I have nested site.properties files, those are files that I need to load when the application is starting and put them to the list (List sites)
Using spring boot mechanisms like @PropertySource, is it possible to search for the files with the same name, map each of them to the Site property class and put to the list?
spring-boot properties-file
add a comment |
Apart from the application.properties, my service also needs to load sites properties. To be more precise, in the sites folder I have nested site.properties files, those are files that I need to load when the application is starting and put them to the list (List sites)
Using spring boot mechanisms like @PropertySource, is it possible to search for the files with the same name, map each of them to the Site property class and put to the list?
spring-boot properties-file
Apart from the application.properties, my service also needs to load sites properties. To be more precise, in the sites folder I have nested site.properties files, those are files that I need to load when the application is starting and put them to the list (List sites)
Using spring boot mechanisms like @PropertySource, is it possible to search for the files with the same name, map each of them to the Site property class and put to the list?
spring-boot properties-file
spring-boot properties-file
edited Nov 13 '18 at 12:59
Roland Weisleder
3,59822139
3,59822139
asked Nov 13 '18 at 12:47
AldonaAldona
12
12
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I'm not sure about
put to the list
but If I understand correctly, you can
search for the files with the same name, map each of them to the Site
property class
by using prefix in properties file as sample below :
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 in resources/com/example/demo/sites/site1/mobile/
site1.mobile.userName = site 1 - mobile - userName
site1.mobile.passWord = site 1 - mobile - passWord
site.properties in resources/com/example/demo/sites/site1/web/
site1.web.userName = site 1 - web- userName
site1.web.passWord = site 1 - web - passWord
site.properties in resources/com/example/demo/sites/site2/mobile/
site2.mobile.userName = site 2 - mobile - userName
site2.mobile.passWord = site 2 - mobile - passWord
site.properties in resources/com/example/demo/sites/site2/web/
site2.web.userName = site 2 - web- userName
site2.web.passWord = site 2 - web - passWord
site.properties in resources/com/example/demo/sites/site3/mobile/
site3.mobile.userName = site 3 - mobile - userName
site3.mobile.passWord = site 3 - mobile - passWord
site.properties in resources/com/example/demo/sites/site3/web/
site3.web.userName = site 3 - web- userName
site3.web.passWord = site 3 - web - passWord
Result :
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53281352%2fspring-boot-load-multiple-properties-files-as-a-list-of-property-objects%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I'm not sure about
put to the list
but If I understand correctly, you can
search for the files with the same name, map each of them to the Site
property class
by using prefix in properties file as sample below :
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 in resources/com/example/demo/sites/site1/mobile/
site1.mobile.userName = site 1 - mobile - userName
site1.mobile.passWord = site 1 - mobile - passWord
site.properties in resources/com/example/demo/sites/site1/web/
site1.web.userName = site 1 - web- userName
site1.web.passWord = site 1 - web - passWord
site.properties in resources/com/example/demo/sites/site2/mobile/
site2.mobile.userName = site 2 - mobile - userName
site2.mobile.passWord = site 2 - mobile - passWord
site.properties in resources/com/example/demo/sites/site2/web/
site2.web.userName = site 2 - web- userName
site2.web.passWord = site 2 - web - passWord
site.properties in resources/com/example/demo/sites/site3/mobile/
site3.mobile.userName = site 3 - mobile - userName
site3.mobile.passWord = site 3 - mobile - passWord
site.properties in resources/com/example/demo/sites/site3/web/
site3.web.userName = site 3 - web- userName
site3.web.passWord = site 3 - web - passWord
Result :
add a comment |
I'm not sure about
put to the list
but If I understand correctly, you can
search for the files with the same name, map each of them to the Site
property class
by using prefix in properties file as sample below :
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 in resources/com/example/demo/sites/site1/mobile/
site1.mobile.userName = site 1 - mobile - userName
site1.mobile.passWord = site 1 - mobile - passWord
site.properties in resources/com/example/demo/sites/site1/web/
site1.web.userName = site 1 - web- userName
site1.web.passWord = site 1 - web - passWord
site.properties in resources/com/example/demo/sites/site2/mobile/
site2.mobile.userName = site 2 - mobile - userName
site2.mobile.passWord = site 2 - mobile - passWord
site.properties in resources/com/example/demo/sites/site2/web/
site2.web.userName = site 2 - web- userName
site2.web.passWord = site 2 - web - passWord
site.properties in resources/com/example/demo/sites/site3/mobile/
site3.mobile.userName = site 3 - mobile - userName
site3.mobile.passWord = site 3 - mobile - passWord
site.properties in resources/com/example/demo/sites/site3/web/
site3.web.userName = site 3 - web- userName
site3.web.passWord = site 3 - web - passWord
Result :
add a comment |
I'm not sure about
put to the list
but If I understand correctly, you can
search for the files with the same name, map each of them to the Site
property class
by using prefix in properties file as sample below :
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 in resources/com/example/demo/sites/site1/mobile/
site1.mobile.userName = site 1 - mobile - userName
site1.mobile.passWord = site 1 - mobile - passWord
site.properties in resources/com/example/demo/sites/site1/web/
site1.web.userName = site 1 - web- userName
site1.web.passWord = site 1 - web - passWord
site.properties in resources/com/example/demo/sites/site2/mobile/
site2.mobile.userName = site 2 - mobile - userName
site2.mobile.passWord = site 2 - mobile - passWord
site.properties in resources/com/example/demo/sites/site2/web/
site2.web.userName = site 2 - web- userName
site2.web.passWord = site 2 - web - passWord
site.properties in resources/com/example/demo/sites/site3/mobile/
site3.mobile.userName = site 3 - mobile - userName
site3.mobile.passWord = site 3 - mobile - passWord
site.properties in resources/com/example/demo/sites/site3/web/
site3.web.userName = site 3 - web- userName
site3.web.passWord = site 3 - web - passWord
Result :
I'm not sure about
put to the list
but If I understand correctly, you can
search for the files with the same name, map each of them to the Site
property class
by using prefix in properties file as sample below :
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 in resources/com/example/demo/sites/site1/mobile/
site1.mobile.userName = site 1 - mobile - userName
site1.mobile.passWord = site 1 - mobile - passWord
site.properties in resources/com/example/demo/sites/site1/web/
site1.web.userName = site 1 - web- userName
site1.web.passWord = site 1 - web - passWord
site.properties in resources/com/example/demo/sites/site2/mobile/
site2.mobile.userName = site 2 - mobile - userName
site2.mobile.passWord = site 2 - mobile - passWord
site.properties in resources/com/example/demo/sites/site2/web/
site2.web.userName = site 2 - web- userName
site2.web.passWord = site 2 - web - passWord
site.properties in resources/com/example/demo/sites/site3/mobile/
site3.mobile.userName = site 3 - mobile - userName
site3.mobile.passWord = site 3 - mobile - passWord
site.properties in resources/com/example/demo/sites/site3/web/
site3.web.userName = site 3 - web- userName
site3.web.passWord = site 3 - web - passWord
Result :
answered Nov 27 '18 at 12:20
VietDDVietDD
35827
35827
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53281352%2fspring-boot-load-multiple-properties-files-as-a-list-of-property-objects%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown