是否有比在每个需要的类中调用loadProperties()更好的方法?在多个类中加载application.properties的正确方法是什么?
public void loadProperties() {
InputStream inputStream;
prop = new Properties();
String propFileName = "application.properties";
inputStream = getClass().getClassLoader().getResourceAsStream(propFileName);
if (inputStream != null) {
try {
prop.load(inputStream);
} catch (IOException e) {
LOGGER.error("Error: ", e);
}
}
}
我知道Spring提供与@Value注释这一功能,但是这仅适用,如果你将类标记为一个@Service。虽然这似乎不是正确方式
为什么需要手动加载它?它也适用于任何Spring bean,只有'@ Service'注释的bean。 –