本文介绍了Spring-Boot多模块无法从另一个模块读取属性文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have searched High and low and still I am unable to find a simple answer to this very annoying problem,

我遵循了这个很棒的指南:具有多服务应用程序的JWT 一切都很好,但是在指南的最后,我们建议创建一个config-service(module),我已经完成了.

I have followed this great guide:JWT with multi service appEverything works great but in the end of the guide we are suggested to create a config-service(module) , which I have done.

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

The problem is that I am unable to override the default configuration of JwtConfig class

项目结构如下:

-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文件中输入什么参数都没有关系,

The problem is that it does not matter what parameters I put in the jwtConfig.properties file,

例如:

   security.jwt.uri=test

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

It will not appear in the JwtConfig bean when the other service loads it.

仅加载默认的@Value.

Only the default @Value's are loaded.

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

can someone have any advice? how may I fix it?Many thanks!

推荐答案

看完Mikhail Kholodkov帖子(谢谢!)

After looking in Mikhail Kholodkov post(Thanks!),

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

The solution is to add the following annotation to the using service Execution point:

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

这篇关于Spring-Boot多模块无法从另一个模块读取属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-22 19:31