spring引导yaml独立配置
是否可以在Spring Boot应用程序之外利用Spring Boot的YAML配置?i、 我们可以只使用YAML配置特性来添加spring引导依赖项吗 我的用例是一个需要配置的小型实用程序项目,YAML方法很合适。如果我将它连接到主项目(一个Spring Boot应用程序),一切都很好。但是,如果我想单独测试这个实用程序项目(简单java应用程序),它不会连接配置。有什么想法吗?可能是我遗漏了一些基本的东西 下面是示例代码片段。下面的软件包是组件扫描的一部分spring引导yaml独立配置,spring,spring-boot,snakeyaml,Spring,Spring Boot,Snakeyaml,是否可以在Spring Boot应用程序之外利用Spring Boot的YAML配置?i、 我们可以只使用YAML配置特性来添加spring引导依赖项吗 我的用例是一个需要配置的小型实用程序项目,YAML方法很合适。如果我将它连接到主项目(一个Spring Boot应用程序),一切都很好。但是,如果我想单独测试这个实用程序项目(简单java应用程序),它不会连接配置。有什么想法吗?可能是我遗漏了一些基本的东西 下面是示例代码片段。下面的软件包是组件扫描的一部分 @Component @Confi
@Component
@ConfigurationProperties(prefix="my.profile")
public class TestConfig {
private List<String> items;
public List<String> getItems() {
return items;
}
public void setItems(List<String> items) {
this.items = items;
}
}
关键是,正如前面提到的
import org.springframework.beans.factory.config.YamlProcessor;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.context.properties.source.ConfigurationPropertySource;
import org.springframework.boot.context.properties.source.MapConfigurationPropertySource;
import org.springframework.core.io.ClassPathResource;
import java.util.Properties;
public class PropertyLoader {
private static Properties properties;
private PropertyLoader() {}
public static Properties load(String... activeProfiles) {
if (properties == null) {
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
factory.setResources(new ClassPathResource("application.yml"));
factory.setDocumentMatchers((profile) -> YamlProcessor.MatchStatus.FOUND); // TODO filter on active profiles
factory.afterPropertiesSet();
properties = factory.getObject();
}
return properties;
}
public static <T> T value(String property, Class<T> target) {
load();
ConfigurationPropertySource propertySource = new MapConfigurationPropertySource(properties);
Binder binder = new Binder(propertySource);
return binder.bind(property.toLowerCase(), target).get();
}
}
Yaml和
@ConfigurationProperties
是两个独立的东西。Yaml支持在Spring本身中可用,@ConfigurationProperties
是Spring boot的一项功能。但是,当作为Spring boot应用程序运行时,相同的代码不会在作为非Spring boot应用程序运行时读取Yaml配置。有什么想法吗?我说yaml支持是可用的,我没有说它是现成的。您必须显式添加以读取yaml文件并作为属性可用。它在Spring 4.1中也可用,因此在早期版本中不起作用。感谢您的回复。。也许我应该把这个问题重新措辞一下。。简而言之,spring boot提供的读取和绑定yml的现成功能不适用于非spring boot应用程序,。。我想知道是否有一种方法可以只使用这一项功能而不将其作为spring boot应用程序运行(原因很简单,我喜欢spring boot,但您并不总是能够灵活地将所有内容迁移到它)。我已经看过YamlPropertiesFactory了,它提供的功能相当原始,与spring boot的开箱即用功能不完全匹配。然后答案很短,没有。。。您必须使用SpringBoot的一部分,并将其迁移到非SpringBoot。
import org.springframework.beans.factory.config.YamlProcessor;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.context.properties.source.ConfigurationPropertySource;
import org.springframework.boot.context.properties.source.MapConfigurationPropertySource;
import org.springframework.core.io.ClassPathResource;
import java.util.Properties;
public class PropertyLoader {
private static Properties properties;
private PropertyLoader() {}
public static Properties load(String... activeProfiles) {
if (properties == null) {
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
factory.setResources(new ClassPathResource("application.yml"));
factory.setDocumentMatchers((profile) -> YamlProcessor.MatchStatus.FOUND); // TODO filter on active profiles
factory.afterPropertiesSet();
properties = factory.getObject();
}
return properties;
}
public static <T> T value(String property, Class<T> target) {
load();
ConfigurationPropertySource propertySource = new MapConfigurationPropertySource(properties);
Binder binder = new Binder(propertySource);
return binder.bind(property.toLowerCase(), target).get();
}
}
List<String> items = PropertyLoader.value("my.profile.items", List.class);