Roi*_*eck 10 maven multi-module spring-boot application.properties java-11
我已经搜索了高低,但仍然无法找到这个非常烦人的问题的简单答案,
我遵循了这个很棒的指南: 带有多服务应用程序的 JWT 一切都很好,但在指南的最后,我们建议创建一个 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 )
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
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*/
Run Code Online (Sandbox Code Playgroud)
问题是我在 jwtConfig.properties 文件中放入什么参数并不重要,
例如:
security.jwt.uri=test
Run Code Online (Sandbox Code Playgroud)
当其他服务加载它时,它不会出现在 JwtConfig bean 中。
仅加载默认的@Value。
有人可以有什么建议吗?我该如何解决?非常感谢!
在查看了 Mikhail Kholodkov 的帖子后(谢谢!),
解决办法是在using service执行点添加如下注解:
@PropertySources({
@PropertySource("classpath:jwtConfig.properties"),
@PropertySource("classpath:app.properties")
})
public class OtherServiceApplication {
public static void main(String[] args) {
SpringApplication.run(OtherServiceApplication.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9969 次 |
| 最近记录: |