(1)如果是properties配置文件
package com.dj.springtest.config.properties; import com.dj.springtest.config.YamlPropertySourceFactory; import lombok.Data; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.PropertySource; /** * User: ldj * Date: 2023/7/14 * Time: 23:27 * Description: 加载自定义配置文件,默认先从bootstrap.properties --> application.properties --> application.yml * 如果上面都没有才会使用自定义的配置文件,如果是yml还需配置ynl解析工厂将yml转成properties */ @Data //@Component (放入容器) @PropertySource(value = "classpath:ldj.properties", encoding = "UTF-8") public class TestConfigProperties { @Value("${spring.test.config.name:ldj}") private String name; @Value("${spring.test.config.age:18}") private Integer age; @Value("${demo.test.gender:女}") private String gender; }
(2)如果是yml配置文件
package com.dj.springtest.config.properties; import com.dj.springtest.config.YamlPropertySourceFactory; import lombok.Data; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.PropertySource; /** * User: ldj * Date: 2023/7/14 * Time: 23:27 * Description: 加载自定义配置文件,默认先从bootstrap.properties --> application.properties --> application.yml * 如果上面都没有才会使用自定义的配置文件,如果是yml还需配置ynl解析工厂将yml转成properties */ @Data //@Component (放入容器) @PropertySource(value = "classpath:ldj.yml", encoding = "UTF-8", factory = YamlPropertySourceFactory.class) public class TestConfigProperties { @Value("${spring.test.config.name:ldj}") private String name; @Value("${spring.test.config.age:18}") private Integer age; @Value("${demo.test.gender:女}") private String gender; }
package com.dj.springtest.config; 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.util.Objects; import java.util.Properties; /** * User: ldj * Date: 2024/4/27 * Time: 7:44 * Description: 在注解指定PropertySourceFactory */ public class YamlPropertySourceFactory implements PropertySourceFactory { @Override public PropertySource<?> createPropertySource(String name, EncodedResource encodedResource) { YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); factory.setResources(encodedResource.getResource()); Properties properties = factory.getObject(); assert properties != null; return new PropertiesPropertySource(Objects.requireNonNull(encodedResource.getResource().getFilename()), properties); } }
或者单独配置成这样,然后结合 @Value就能获取yml配置文件的参数
@Bean public static PropertySourcesPlaceholderConfigurer placeholderConfigurer() { PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer(); YamlPropertiesFactoryBean yamlPropertiesFactoryBean = new YamlPropertiesFactoryBean(); yamlPropertiesFactoryBean.setResources(new ClassPathResource("ldj.yml")); configurer.setProperties(Objects.requireNonNull(yamlPropertiesFactoryBean.getObject())); return configurer; }
(3)放入容器 使用@Import放入容器,但是不能使用 @EnableConfigurationProperties
package com.dj.springtest.config; import com.dj.springtest.config.properties.TestConfigProperties; import com.dj.springtest.service.TestService; import com.dj.springtest.service.impl.TestServiceImpl; import lombok.Data; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; /** * User: ldj * Date: 2023/7/14 * Time: 23:28 * Description: 使用@Import放入容器,但是不能使用 @EnableConfigurationProperties */ @Data @Configuration @Import(value = {TestConfigProperties.class}) // 也要遵循 bootstrap.properties --> application.properties --> application.yml--> ldj.properties //@EnableConfigurationProperties(value = {TestConfigProperties.class}) //失败!! 属性值必须配置bootstrap.properties、application.properties 或者application.yml文件才可以 public class TestConfig { @Bean @ConditionalOnMissingBean(TestService.class) public TestServiceImpl testService(TestConfigProperties properties) { return new TestServiceImpl(properties.getName(), properties.getGender(), properties.getAge()); } }
补充:在代码里动态获取配置参数,还可以使用环境对象
@Autowired private Environment environment; public void test() { String value = environment.getProperty("demo.test.gender"); }