对于不同的操作系统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,但这是一个条件。也许他会以某种方式提供帮助。
发布于 2019-01-08 23:05:16
(1)为OS .Use system property os.name定义枚举,以确定当前OS:
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
发布于 2019-01-08 22:22:46
@Conditional对于确定操作系统非常有用,因此您必须定义条件类。
作为一种更简单的方法,您可以使用很好的旧if语句来确定操作系统。假设您有两个不同的文件,即建议的windows.properties和unix.properties,创建配置类以确定操作系统并加载适当的.properties文件。
configuration类的代码如下所示。
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;
}
}https://stackoverflow.com/questions/54091968
复制相似问题