@Configuration public class ApplicationConfigurer { private static Logger logger = Logger.getLogger(ApplicationConfigurer.class); public static final String SPRING_CONFIG_LOCATION = "spring.config.location"; /** * 自定义配置加载,方法定义为static的,保证优先加载 * @return */ @Bean public static PropertyPlaceholderConfigurer properties() { final PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); ppc.setIgnoreResourceNotFound(true); final List<Resource> resourceLst = new ArrayList<Resource>(); if(System.getProperty(SPRING_CONFIG_LOCATION) != null){ String configFilePath = System.getProperty(SPRING_CONFIG_LOCATION); String[] configFiles = configFilePath.split(",|;"); FileSystemResource res =null; for (String configFile : configFiles) { if (configFile.startsWith("file:")){ resourceLst.add(new FileSystemResource(configFile)); }else { resourceLst.add( new ClassPathResource(configFile)); } } }else { resourceLst.add(new ClassPathResource("config/application.properties")); resourceLst.add(new ClassPathResource("config/kafka.properties")); } ppc.setLocations(resourceLst.toArray(new Resource[]{})); return ppc; } }
spring boot 自定义 PropertyPlaceholderConfigurer
最新推荐文章于 2026-05-04 15:42:43 发布
该博客介绍了如何在Spring Boot中自定义PropertyPlaceholderConfigurer,通过编写@Configuration类和@Bean注解的方法来设置配置文件加载位置。方法设为static以确保优先加载。如果系统属性中指定了`spring.config.location`,则从指定路径加载配置文件,否则默认加载`config/application.properties`和`config/kafka.properties`。

1132

被折叠的 条评论
为什么被折叠?



