除了application.properties,我的服务还需要加载站点属性。更准确地说,在 sites 文件夹中,我嵌套了site.properties文件,这些文件是我在应用程序启动时需要加载的文件,并将其放入列表中(列出网站)
使用诸如@PropertySource之类的spring boot机制,是否可以搜索具有相同名称的文件,然后将每个文件映射到Site属性类并放入列表?
答案 0 :(得分:0)
我不确定
放入列表
但是,如果我理解正确,则可以
搜索具有相同名称的文件,然后将每个文件映射到站点 财产类别
通过使用属性文件中的前缀作为下面的示例:
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