SpringBoot 解决多模块配置文件@Value()注解找不到配置文件问题解决方案

  1. 项目结构

启动模块为spi_wxcp, spi_ldap为ldap功能模块该模块为连接处理AD逻辑模块。
2. spi_ldap配置类信息

1
2
3
4
5
6
7
8
9
10
11
@Configuration
@PropertySource(value = "classpath:application-ldap.yml",encoding = "utf-8")
public class LdapConfig {
@Value("${spring.ldap.urls}")
private String ldapUrl;
@Value("${spring.ldap.username}")
private String userName;
@Value("${spring.ldap.password}")
private String passWord;
@Value("${spring.ldap.base}")
private String base;

3.报错信息

1
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'wxCpConfiguration' defined in file [/Users/leslie/Desktop/Project/Spi_WxCP_Ldap/spi_wxcp/target/classes/com/spi/wxcp/config/WxCpConfiguration.class]: Unsatisfied dependency expressed through constructor parameter 3; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'menuHandler': Unsatisfied dependency expressed through field 'ldapService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'ldapServiceImpl': Unsatisfied dependency expressed through field 'ldapTemplate'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ldapConfig': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.ldap.urls' in value "${spring.ldap.urls}
  1. 问题分析及解决方案
    从报错信息看到是java.lang.IllegalArgumentException: Could not resolve placeholder ‘spring.ldap.urls’ in value “${spring.ldap.urls}”@value注解无法读取到配置文件,我们在配置类中声明了配置文件为application-ldap.yml,但是在多模块配置文件中除非为启动模块,使用@Value注解的读取的配置文件建议不要为yml格式,解决方法就是将配置文件yml改成properties文件即可。

修改后启动项目

没有报错配置文件注入成功