【问题标题】:How can I avoid to call properties file and properties of it every time?如何避免每次调用属性文件及其属性?
【发布时间】:2019-01-15 19:01:17
【问题描述】:

我在下面用 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、analyzeManager、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
...

【问题讨论】:

  • 为什么你不能使用spring`s propertyplace holder Sample
  • @Barath ,属性文件不是常量。它取自控制台作为参数。
  • 一旦属性文件作为参数传递,属性是不可变的。您可以使用属性占位符,它将属性直接加载到 spring 的环境中。你可以让春天发挥它的魔力。
  • @Barath,请给我一些关于我的例子的实现。就像,我们的自定义属性上有用户名和 startDate。我们如何为这个例子集成它的弹簧属性?
  • Utils 应该是一个 Spring 托管的 bean,其中属性在应用程序初始化期间加载一次。另外,考虑添加getUserName()getPassword() 等来隐藏/封装属性键而不暴露属性对象。根据您的描述,Utils 应命名为 CredentialsProvider

标签: java spring spring-boot properties


【解决方案1】:

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

@PropertySource("${credential}")

credential 变量(属性文件的路径)将从命令行参数中读取。然后你可以像这样使用@Value 注解:

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

【讨论】:

  • 这可以是其他解决方案,但要实现它,需要 PropertySourcesPlaceholderConfigurer。因为找不到 ${credential} 占位符。另一个问题是凭据不是系统属性,因此您应该将其设置为系统属性。
【解决方案2】:

问题解决如下:

@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 的帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-23
    • 1970-01-01
    • 2013-11-10
    • 1970-01-01
    • 1970-01-01
    • 2023-02-01
    • 2020-11-20
    相关资源
    最近更新 更多