gpt4 book ai didi

java - spring 的库(JAR)属性默认值

转载 作者:行者123 更新时间:2023-12-02 03:58:45 24 4
gpt4 key购买 nike

我有一个需要发送电子邮件的应用程序。我稍后可能需要在第二个应用程序中发送电子邮件,因此我尝试创建一个库来重用它。

我的应用程序是使用 Spring-boot 完成的。我的库只包含 Spring-core、Spring-context 和 Spring-test。

这是我的库中的 EmailUtils:

@Named
public class EmailUtils {

@Value("${from}")
private String from;

@Value("${to}")
private String to;
...
}

我想要一个默认文件 application.properties 并在库中:

from=default-from
to=default-to

现在我想如果我的应用程序 spring boot 执行类似的操作:

@Service
public void EmailService {

@Inject
private EmailUtils emailUtils;
...
}

我希望如果我不在我的应用程序中定义新的 application.properties 来从库中获取默认值,但可以通过在我的 Spring-boot 应用程序中使用 application.properties 来覆盖它们。

信息:我的 Spring-boot 应用程序有 ScanComponent 包(仅提一下)。但我的图书馆什么也没有。我不知道是否必须使用 beans.xml 并定义 ContextPlaceHolder ...

我有点失落,也许有更好的方法?

谢谢。

最佳答案

如果我正确理解您的问题 - 您想为您的 @Value 带注释的属性指定默认值。您只需使用格式 @Value("${property_key:default value}") 即可完成此操作,因此如果您希望默认 from 值为 me@me.comtoyou@you.com 您的代码将如下:

@Named
public class EmailUtils {

@Value("${from:me@me.com}")
private String from;

@Value("${to:you@you.com}")
private String to;
...
}

关于java - spring 的库(JAR)属性默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35141109/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com