Spring-Boot多模块无法从另一个模块读取属性文件

时间:2018-10-05 21:00:13

标签: maven spring-boot multi-module application.properties java-11

我已经搜索了高点和低点,但仍然找不到这个非常烦人的问题的简单答案,

我遵循了这个很棒的指南: JWT with multi service app 一切都很好,但是在指南的最后,我们建议创建一个config-service(module),我已经完成了。

问题是我无法覆盖JwtConfig类的默认配置

项目结构如下:

-config-service 

    | JwtConfig.java
     \
        | resources 
        \
         | jwtConfig.properties

 -other-service (add dependency in the pom file of the config-service)
     |
       someOtherclass.java (import the JwtConfig class & using @Bean to initialize )

JwtConfig类:

/*all the imports*/ 
@PropertySource(value = "classpath:jwtConfig.properties")
public class JwtConfig {

@Value("${security.jwt.uri:/auth/**}")
private String Uri;

@Value("${security.jwt.header:Authorization}")
private String header;

@Value("${security.jwt.prefix:Bearer }")
private String prefix;

@Value("${security.jwt.expiration:#{24*60*60}}")
private int expiration;

@Value("${security.jwt.secret:JwtSecretKey}")
private String secret;

 //getters

someOtherclass.java:

/*imports*/

@Configuration
@EnableWebSecurity
public class SecurityCredentialsConfig  extends WebSecurityConfigurerAdapter 
{ 

   private JwtConfig jwtConfig; 

   @Autowired
   public void setJwtConfig(JwtConfig jwtConfig) {
       this.jwtConfig = jwtConfig;
   }
   @Bean
   public JwtConfig jwtConfig() {
    return new JwtConfig();
   }
   /*other code*/

问题在于我在jwtConfig.properties文件中输入什么参数都没有关系,

例如:

   security.jwt.uri=test 

当其他服务加载它时,它不会出现在JwtConfig bean中。

仅加载默认的@Value。

有人可以提些建议吗?我该如何解决? 非常感谢!

2 个答案:

答案 0 :(得分:1)

看完Mikhail Kholodkov的帖子(谢谢!)

解决方案是将以下注释添加到using服务执行点:

 @PropertySources({
    @PropertySource("classpath:jwtConfig.properties"),
    @PropertySource("classpath:app.properties")
})
public class OtherServiceApplication {
public static void main(String[] args) {
    SpringApplication.run(OtherServiceApplication.class, args);
    }
}

答案 1 :(得分:1)

我认为代替使用@PropertySources,更好的方法和更合适的方法是在包含“主要方法”的模块中使用@ComponentScan。由于您将需要一个JWTConfiguration类的实例,而不是实际的 .property 文件,因此,建议您公开该bean并使springboot从另一个模块对其进行扫描,而不是公开该属性文件(因为这样会使另一个模块中的jwtConfiguration.java文件变得毫无用处。)因此,您可以尝试这样的操作

假设我们在主模块(只有pom)内部有两个模块-Module1和Module2。我想您知道演习是无代码模块只是将应用程序打包为“ pom”,并在

中描述了相关模块

您的主要pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>XXX</groupId>
    <artifactId>XXXX</artifactId>
    <packaging>pom</packaging>
    <version>1.0</version>
    <name>ws-cms-engine</name>
    <url>http://maven.apache.org</url>
    <properties>
        <spring-boot.version>2.0.0.RELEASE</spring-boot.version>
        <spring-kafka.version>2.2.3.RELEASE</spring-kafka.version>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <!-- Import dependency management from Spring Boot -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
............
............
    <modules>
      <module>Module1</module>
      <module>Module2</module>
    </modules>

现在让我们考虑一下您的JWTConfiguration在Module1中,并且它使用了声明的资源文件夹中的属性文件-application.properties

示例JWTConfiguation.java文件

package common.module2.config
@Configuration
@PropertySources("classpath:application.properties")
public class JWTConfiguration{

  @Value("${config1}")
  private String someConfig;


}

现在,如果您的Module2具有需要使用此配置的主类,那么类似的事情可能会有意义

我们需要使SpringBoot容器从module1中声明的bean中读取,而不是读取实际的属性文件

@ComponentScan(basepackages={"common.module2.config", "common.module1.this.config"})
@SpringBootApplication
public class Application(){
    public static void main(String args[]){
     SpringApplication.run(Application.class);
   }
}

所以在这里,我们通知spring容器在启动和初始化时需要扫描module2包中声明的bean

现在,您可以在所需的服务中自动装配bean并使用它

@Service
public class SampleService{
   @Autowired
   JWTConfiguration config;

}

这应该自动连接JWTConfiguration的托管实例供您使用