在 spring 中串联配置文件消息
concatenating profile messages in spring
所以我想在 spring 引导中连接来自配置文件的两条消息。
这是我当前的代码(它不起作用,因为字段为空):
@Profile("fetchGame")
@Service
public class FetchGameApiService {
@Value("${game.api.url}")
private static String LINK;
@Value("${game.api.key}")
private static String KEY;
private static Integer gameId = 1;
private static final String URL = LINK + "/" + gameId + "?key=" + KEY;
//somelogic
}
这是我的个人资料页面:
game.api.url = https://api.rawg.io/api/games
game.api.key = 250b2de5e7734f638760ae2bad8bd29f
this_IS_The_Correct_Url = https://api.rawg.io/api/games/1?key=250b2de5e7734f638760ae2bad8bd29f
请注意,我在 app.properties 中将活动配置文件设置为:spring.profiles.active = fetchGame
我的问题是:如何将 spring 个配置文件中的两个字符串连接成一个字符串的正确方法?无需大量代码,简单易懂。
首先,spring 配置文件与向字段或 method/constructor 参数注入值无关。
Spring profiles 允许有条件地配置应用程序上下文。
这是要查看的列表:
- 确保保留字段 non-static。
- 检查
PropertySourcesPlaceholderConfigurer
bean 是否在上下文中注册
- 或使用
@PropertySource("classpath:application.properties")
设置自定义 属性 文件以解析来自的值。
这是功能上的reference documentation。
我建议学习并采用类型安全的方式来处理 w/ configuration properties w/ @ConfigurationProperties
注释。
所以我想在 spring 引导中连接来自配置文件的两条消息。 这是我当前的代码(它不起作用,因为字段为空):
@Profile("fetchGame")
@Service
public class FetchGameApiService {
@Value("${game.api.url}")
private static String LINK;
@Value("${game.api.key}")
private static String KEY;
private static Integer gameId = 1;
private static final String URL = LINK + "/" + gameId + "?key=" + KEY;
//somelogic
}
这是我的个人资料页面:
game.api.url = https://api.rawg.io/api/games
game.api.key = 250b2de5e7734f638760ae2bad8bd29f
this_IS_The_Correct_Url = https://api.rawg.io/api/games/1?key=250b2de5e7734f638760ae2bad8bd29f
请注意,我在 app.properties 中将活动配置文件设置为:spring.profiles.active = fetchGame 我的问题是:如何将 spring 个配置文件中的两个字符串连接成一个字符串的正确方法?无需大量代码,简单易懂。
首先,spring 配置文件与向字段或 method/constructor 参数注入值无关。 Spring profiles 允许有条件地配置应用程序上下文。
这是要查看的列表:
- 确保保留字段 non-static。
- 检查
PropertySourcesPlaceholderConfigurer
bean 是否在上下文中注册 - 或使用
@PropertySource("classpath:application.properties")
设置自定义 属性 文件以解析来自的值。
这是功能上的reference documentation。
我建议学习并采用类型安全的方式来处理 w/ configuration properties w/ @ConfigurationProperties
注释。