繁体   English   中英

如何避免每次都调用属性文件及其属性?

[英]How can I avoid to call properties file and properties of it every time?

我在下面用args编写了spring boot应用程序。

java jar ... --credential="path\credentials.properties"

从控制台运行。 我从控制台获取属性文件路径,并在需要属性时始终获取属性文件路径,首先加载属性文件,然后使用密钥获取属性。 我该如何避免呢? 我只想一次加载属性文件,以后总是想使用这个第一个加载的文件。 我不想一次又一次地加载。 那样,我不想调用它的属性。

@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();
    }
}

}

在每个服务(authService,analyzerManager,writerService)中,我都根据args加载属性。 稍后继续我的过程。

为了加载属性,我创建了一个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;
    }
}

属性文件包含以下内容:

- username
- password
- outputFileName
- startDate
- endDate
...

您可以将属性文件注册为SpringBoot属性源

@PropertySource("${credential}")

credential变量(属性文件的路径)将从命令行参数中读取。 然后,您可以使用@Value注释,如下所示:

@Value("${property_name}") String property;

问题解决如下:

@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();
    }
}

}

添加了新的配置文件,如下所示:

@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;
    }
}

然后从其他类调用属性:

@Autowired
private ConfigProperties configProperties;

并像这样调用属性:

configProperties.getUserName();

非常感谢您的帮助@Barath,@Andrew S

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM