有两个用于不同操作系统windows.properties
和unix.properties
的配置文件。
有一个配置:
@Configuration
@ConfigurationProperties (prefix = "storage")
public class StorageProperties {
private String root;
private String sitesDirName;
private String avatarsDirName;
private String screenshotsDirName;
@PostConstruct
public void postConstruct () {
}
}
如何制作文件以便根据操作系统加载?我碰到了@Conditional
,但这是一个条件。也许他会以某种方式帮助您。
答案 0 :(得分:1)
(1)定义操作系统的枚举。使用系统属性os.name
确定当前的操作系统:
public enum OS {
WINDOWS, UNIX, MAC, UNKNOWN;
public static OS currentOS() {
String OS = System.getProperty("os.name").toLowerCase();
if (OS.indexOf("win") >= 0) {
return WINDOWS;
} else if (OS.indexOf("nix") >= 0 || OS.indexOf("nux") >= 0 || OS.indexOf("aix") > 0) {
return UNIX;
} else if ((OS.indexOf("mac") >= 0)) {
return MAC;
} else {
return UNKNOWN;
}
}
}
(2)实现ConditionalOnOS
:
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OsCondition.class)
public @interface ConditionalOnOS {
OS os();
}
public class OsCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
MultiValueMap<String, Object> attrs = metadata.getAllAnnotationAttributes(ConditionalOnOS.class.getName());
if (attrs != null) {
Object os = attrs.getFirst("os");
if (os != null && os instanceof OS) {
if (OS.currentOS().equals(((OS) os))) {
return true;
}
}
}
return false;
}
}
(3)为不同的操作系统配置@ConfigurationProperties
。使用@PropertySource
定义不同操作系统的属性文件路径:
@ConfigurationProperties(prefix = "storage")
public static class StorageProperties {
private String root;
private String sitesDirName;
private String avatarsDirName;
private String screenshotsDirName;
@Configuration
@PropertySource("classpath:windows.properties")
@ConditionalOnOS(os = OS.WINDOWS)
public static class WindowsStrogeProperties extends StorageProperties {
}
@Configuration
@PropertySource("classpath:unix.properties")
@ConditionalOnOS(os = OS.UNIX)
public static class UnixStrogeProperties extends StorageProperties {
}
}
(4)向客户端注入StorageProperties
答案 1 :(得分:0)
@Conditional
对于确定操作系统非常有用,因此您必须定义条件类。
作为一种较短的方法,您可以使用旧的if语句来确定操作系统。假定您有两个不同的文件,如建议的windows.properties
和unix.properties
,创建配置类以确定操作系统并加载适当的.properties
文件。
配置类的代码如下所示。
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.ClassPathResource;
import com.sun.javafx.PlatformUtil;
@Configuration
public class OSConfiguration {
@Bean
public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() {
String osName = "";
if (PlatformUtil.isWindows()) {
osName = "windows";
} else if (PlatformUtil.isUnix()) {
osName = "unix";
} else if (PlatformUtil.isMac()) {
osName = "mac";
}
String propertiesFilename = osName + ".properties";
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
configurer.setLocation(new ClassPathResource(propertiesFilename));
return configurer;
}
}