gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 18:18:15 24 4
gpt4 key购买 nike

我搜索了高低,但我仍然无法找到这个非常烦人的问题的简单答案,

我遵循了这个伟大的指南:
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。

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

最佳答案

在查看 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);
}
}

关于maven - Spring-Boot 多模块无法从另一个模块读取属性文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52673216/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com