/**
*
*/
package cn.com.btmu.art.framework.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import java.io.Serializable;
/**
* 获取相关的spring bean定义
*
*/
public class FactoryBeanUtil implements ApplicationContextAware, Serializable {
private static final Logger logger = LoggerFactory.getLogger(FactoryBeanUtil.class);
private static final long serialVersionUID = 1L;
private static ApplicationContext applicationContext;
@SuppressWarnings("unchecked")
//根据类class,spring bean获取
public static <T> T getBean(Class<T> clazz,String beanName){
// if (logger.isDebugEnabled()) {
// logger.debug(FactoryBeanUtil.class.getSimpleName() + ": is invoking getBean , class:" + clazz + " beanName:" + beanName);
// }
T bean = null;
try {
bean = (T)applicationContext.getBean(beanName);
} catch(Exception e) {
logger.warn("No bean named " + beanName + " is defined");
}
return bean;
}
@SuppressWarnings("unchecked")
//根据spring bean获取
public static Object getBean(String beanName){
// if (logger.isDebugEnabled()) {
// logger.debug(FactoryBeanUtil.class.getSimpleName() + ": is invoking getBean , beanName:" + beanName);
// }
Object bean = null;
try {
bean = applicationContext.getBean(beanName);
} catch(Exception e) {
logger.warn("No bean named " + beanName + " is defined");
}
return bean;
}
private static String[] getBeanNamesByType(Class classType) {
// if (logger.isDebugEnabled()) {
// logger.debug(FactoryBeanUtil.class.getSimpleName() + ": is invoking getBean , classType:" + classType);
// }
String[] beanNames = null;
try {
beanNames = applicationContext.getBeanNamesForType(classType);
} catch(Exception e) {
logger.warn("No bean with classType " + classType + " is defined");
}
return beanNames;
}
public static <T> T getBeanByType(Class<T> classType) {
// if (logger.isDebugEnabled()) {
// logger.debug(FactoryBeanUtil.class.getSimpleName() + ": is invoking getBean , classType:" + classType);
// }
String[] beanNames = null;
try {
beanNames = applicationContext.getBeanNamesForType(classType);
} catch(Exception e) {
logger.warn("No bean with classType " + classType + " is defined");
}
if (beanNames.length == 0) {
logger.warn("No bean with classType " + classType + " is defined");
return null;
}
if (beanNames.length > 1) {
logger.error("there are " + beanNames.length + " bean with the classType " + classType);
return null;
}
return getBean(classType , beanNames[0]);
}
@SuppressWarnings("static-access")
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
if (logger.isDebugEnabled()) {
logger.debug(this.getClass().getSimpleName() + ": init applicationContext");
}
this.applicationContext = applicationContext;
}
/**
* 获取应用名称
* @return
*/
public static String getAppName() {
if (getExtendApplicationContext() != null) {
return getExtendApplicationContext().getAppName();
}
return null;
}
/**
* 获取应用工作路径
* @return
*/
public static String getApplicationWorkPath() {
if (getExtendApplicationContext() != null) {
return getExtendApplicationContext().getApplicationWorkPath();
}
return null;
}
/**
* 获取Home路径
* @return
*/
public static String getHomePath() {
if (getExtendApplicationContext() != null) {
return getExtendApplicationContext().getEOS_HOME();
}
return null;
}
/**
* 获取应用配置文件夹路径(系统、用户)
* @return
*/
public static String getApplicationConfigPath() {
if (getExtendApplicationContext() != null) {
return getExtendApplicationContext().getApplicationConfigPath();
}
return null;
}
/**
* 获取应用系统相关构件存储库路径
* @return
*/
public static String getApplicationSystemWorkingPath() {
if (getExtendApplicationContext() != null) {
return getExtendApplicationContext().getApplicationSystemWorkingPath();
}
return null;
}
/**
* 获取应用用户相关构件存储库路径
*/
public static String getApplicationUserWorkingPath() {
if (getExtendApplicationContext() != null) {
return getExtendApplicationContext().getApplicationUserWorkingPath();
}
return null;
}
/**
* 获取应用EAR所在物理目录
* @return
*/
public static String getEarRealPath() {
if (getExtendApplicationContext() != null) {
return getExtendApplicationContext().getEarRealPath();
}
return null;
}
/**
* 获取EOS HOME
* @return
*/
public static String getEosHome() {
if (getExtendApplicationContext() != null) {
return getExtendApplicationContext().getEOS_HOME();
}
return null;
}
/**
* 获取封装的应用上下文容器
* @return
*/
public static com.eos.runtime.core.ApplicationContext getExtendApplicationContext() {
return com.eos.runtime.core.ApplicationContext.getInstance();
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
}