In one of our legacy code base, I found the following pattern being used which seems to be a bit fragile. Consider the following Spring Bean:
@Component
public class PropsProvider {
private static Properties props;
@Inject
PropsProvider(Configuration config) {
PropsProvider.props = ConfigurationConverter.getProperties(config);
}
public static String getProperty(String key) {
return props.getProperty(key);
}
}
which will then be referred inside a plain Java class in a static way as shown below:
public class UrlUtil {
private static String IMAGE_URL;
static {
IMAGE_URL = PropsProvider.getProperty("image_url");
}
private UrlUtil() {}
public static void String getImageUrl() {
return IMAGE_URL;
}
}
Can there be an instance where during the server start-up time, the static block in UrlUtil
gets executed before the Spring bean PropsProvider
gets initalized leading to UrlUtil.getImageUrl()
returning null to some caller class?