已找到解决方案。@ExtendWith(SpringExtension.class) @EnableConfigurationProperties(CourseConfig.class) @PropertySource(value = "classpath:application.yml", factory = YamlPropertySourceFactory.class) class CourseConfigTest { }这种即可,不过默认PropertySource只能解析properties这种配置文件。import org.springframework.beans.factory.config.YamlPropertiesFactoryBean; import org.springframework.core.env.PropertiesPropertySource; import org.springframework.core.env.PropertySource; import org.springframework.core.io.support.EncodedResource; import org.springframework.core.io.support.PropertySourceFactory; import java.io.IOException; import java.util.Properties; public class YamlPropertySourceFactory implements PropertySourceFactory { @Override public PropertySource<?> createPropertySource(String name, EncodedResource encodedResource) throws IOException { YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); factory.setResources(encodedResource.getResource()); Properties properties = factory.getObject(); return new PropertiesPropertySource(encodedResource.getResource().getFilename(), properties); } }参考来源: https://www.baeldung.com/spri...
已找到解决方案。
这种即可,不过默认
PropertySource
只能解析properties
这种配置文件。参考来源: https://www.baeldung.com/spri...