[英]How can I avoid to call properties file and properties of it every time?
I wrote below spring boot application with args. 我在下面用args编写了spring boot应用程序。
java jar ... --credential="path\credentials.properties"
Run like that from console. 从控制台运行。 I get the properties file path from console and all time when I need a property, firstly I load properties file and later get my property with my key.
我从控制台获取属性文件路径,并在需要属性时始终获取属性文件路径,首先加载属性文件,然后使用密钥获取属性。 How can I avoid it?
我该如何避免呢? I just want to load properties file one time and later always I want to use this first loaded file.
我只想一次加载属性文件,以后总是想使用这个第一个加载的文件。 I don't want to load again and again.
我不想一次又一次地加载。 Like that, I don't want to call properties of it.
那样,我不想调用它的属性。
@SpringBootApplication
@Slf4j
class ProRunner implements CommandLineRunner {
@Autowired
AnalyzeManager analyzeManager;
@Autowired
AuthService authService;
@Autowired
WriterService writerService;
static void main(String[] args) {
SpringApplication.run(ProRunner.class, args);
}
@Override
void run(String... args) throws Exception {
try {
String token = authService.postEntity("url", args).token;
Map dataSet = analyzeManager.bulkCreate(args);
writerService.write(args, dataSet)
} catch (Exception e) {
e.printStackTrace();
}
}
}
In every service (authService, analyzeManager, writerService), I load properties according to args. 在每个服务(authService,analyzerManager,writerService)中,我都根据args加载属性。 Later go on with my process.
稍后继续我的过程。
To load properties, I create a Util method and I call it every time. 为了加载属性,我创建了一个Util方法,每次都调用它。
public class Utils {
public static Properties getProperties(String...args) throws IOException {
File file = new File(args[0]);
Properties properties = new Properties();
InputStream in = new FileInputStream(file);
properties.load(in);
return properties;
}
}
Properties file contains things like that: 属性文件包含以下内容:
- username
- password
- outputFileName
- startDate
- endDate
...
You can register properties file as SpringBoot propserties source 您可以将属性文件注册为SpringBoot属性源
@PropertySource("${credential}")
and credential
variable (path to your properties file) will be read from command line arguments. 和
credential
变量(属性文件的路径)将从命令行参数中读取。 Then you can use @Value annotation like this: 然后,您可以使用@Value注释,如下所示:
@Value("${property_name}") String property;
Problem is solved like below: 问题解决如下:
@SpringBootApplication
@Slf4j
class ProRunner implements CommandLineRunner {
@Autowired
AnalyzeManager analyzeManager;
@Autowired
AuthService authService;
@Autowired
WriterService writerService;
static void main(String[] args) {
System.setProperty("spring.config.additional-location","credential from args");
SpringApplication.run(ProRunner.class, args);
}
@Override
void run(String... args) throws Exception {
try {
String token = authService.postEntity("url").token;
Map dataSet = analyzeManager.bulkCreate();
writerService.write(dataSet)
} catch (Exception e) {
e.printStackTrace();
}
}
}
Added new configuration file like below: 添加了新的配置文件,如下所示:
@Configuration
public class ConfigProperties {
@Value("${cred.username}")
private String userName;
@Value("${cred.password}")
private String password;
@Value("${date.startDate}")
private String startDate;
@Value("{team.id}")
private String teamId;
public String getUserName() {
return userName;
}
public String getPassword() {
return password;
}
public String getStartDate() {
return startDate;
}
public String getTeamId() {
return teamId;
}
}
And later to call properties from other class: 然后从其他类调用属性:
@Autowired
private ConfigProperties configProperties;
And call property like that: 并像这样调用属性:
configProperties.getUserName();
Thank you very much for your helps @Barath, @Andrew S 非常感谢您的帮助@Barath,@Andrew S
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.