All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.antelopeframework.spring.ApplicationContextContainer Maven / Gradle / Ivy

The newest version!
package com.github.antelopeframework.spring;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang3.StringUtils;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.github.antelopeframework.util.component.AbstractLifeCycle;

/**
 * 
 * @author yangzhi
 */
@Slf4j
public class ApplicationContextContainer extends AbstractLifeCycle {
	private static ClassPathXmlApplicationContext context = null;
	
	private final String[] configLocations;
	
	public ApplicationContextContainer(String[] configLocations) {
		if (configLocations == null || configLocations.length == 0) {
			throw new IllegalArgumentException("Spring config location is null or empty.");
		}
		
		this.configLocations = configLocations;
	}
	
	@Override
	public void doStart() throws Exception {
		if (log.isDebugEnabled()) {
			log.debug("Initialized ApplicationContext with : {}", StringUtils.join(configLocations, ","));
		}
		
		context = new ClassPathXmlApplicationContext(configLocations);
        context.start();
        
        // 主动初始化SpringContextHolder中的applicationContext的属性.
        SpringContextHolder.applicationContext = context;
	}
	
	@Override
	public void doStop() throws Exception {
		try {
			if (context != null) {
				context.stop();
				context.close();
				context = null;
			}
		} catch (Throwable e) {
			log.error(e.getMessage(), e);
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy