Сначала давайте посмотрим на класс полезности (наиболее Javadoc была удалена, чтобы просто пример):Является ли следующий класс утилиты потокобезопасным?
public class ApplicationContextUtils {
/**
* The application context; care should be taken to ensure that 1) this
* variable is assigned exactly once (in the
* {@link #setContext(ApplicationContext)} method, 2) the context is never
* reassigned to {@code null}, 3) access to the field is thread-safe (no race
* conditions can occur)
*/
private static ApplicationContext context = null;
public static ApplicationContext getContext() {
if (!isInitialized()) {
throw new IllegalStateException(
"Context not initialized yet! (Has the "
+ "ApplicationContextProviderBean definition been configured "
+ "properly and has the web application finished "
+ "loading before you invoked this method?)");
}
return context;
}
public static boolean isInitialized() {
return context == null;
}
@SuppressWarnings("unchecked")
public static <T> T getBean(final String name, final Class<T> requiredType) {
if (requiredType == null) {
throw new IllegalArgumentException("requiredType is null");
}
return (T) getContext().getBean(name, requiredType);
}
static synchronized void setContext(final ApplicationContext theContext) {
if (theContext == null) {
throw new IllegalArgumentException("theContext is null");
}
if (context != null) {
throw new IllegalStateException(
"ApplicationContext already initialized: it cannot be done twice!");
}
context = theContext;
}
private ApplicationContextUtils() {
throw new AssertionError(); // NON-INSTANTIABLE UTILITY CLASS
}
}
Наконец, существует следующий помощник Spring управляемый компонент, который на самом деле вызывает метод «SetContext»:
public final class ApplicationContextProviderBean implements
ApplicationContextAware {
public void setApplicationContext(
final ApplicationContext applicationContext) throws BeansException {
ApplicationContextUtils.setContext(applicationContext);
}
}
Весна вызовет метод setApplicationContext один раз после запуска приложения. Предполагая, что nincompoop ранее не назывался ApplicationContextUtils.setContext(), который должен блокировать ссылку на контекст в классе утилиты, что позволяет вызовам getContext() для успеха (это означает, что isInitialized() возвращает true).
Я просто хочу знать, нарушает ли этот класс какие-либо принципы хорошей практики кодирования, в частности, в отношении безопасности потоков (но можно найти другие глупости).
Спасибо, что помогли мне стать лучшим программистом, StackOverflow!
С уважением, LES
P.S. Я не заходил в , почему Мне нужен этот класс утилиты - давайте достаточно, чтобы у меня действительно была законная необходимость доступа к нему из статического контекста в любом месте приложения (после того, как, конечно же, загрузился весенний контекст).
«большинство javadoc было удалено просто в качестве примера» -> lol – Fortega