I have a bean as below,

public class SpringContextUtil implements ApplicationContextAware {

private static ApplicationContext context = null;

public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.context = applicationContext;

public static <T> T getBean(String beanName) {
return (T) context.getBean(beanName);

public static String getActiveProfile() {
return context.getEnvironment().getActiveProfiles()[0];

it is used by a plain class,

public class ConfigUtil
String env = SpringContextUtil.getActiveProfile();

public static getVal(String key){...}

If the Spring Bean SpringContextUtil is initialized after the plain class ConfigUtil, then it is not OK.
So I want to know how Spring decide which one is initilialized earlier? For example, will the invocation of SpringContextUtil.getActiveProfile() trigger the initialization of the Spring bean? or the Spring Bean is only initialized but havn't been injected applicationContext yet, thus leads to a Null Pointer Exeception?