com.github.dennisit.vplus.data.spring.context.SpringContext Maven / Gradle / Ivy
/*--------------------------------------------------------------------------
* Copyright (c) 2010-2020, Elon.su All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the elon developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author: Elon.su, you can also mail [email protected]
*--------------------------------------------------------------------------
*/
package com.github.dennisit.vplus.data.spring.context;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Deprecated
@Component
public class SpringContext implements ApplicationContextAware {
private static ApplicationContext context;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContext.context = applicationContext;
}
/**
* 根据bean名称获取bean
*
* @param name bean名称
* @param 返回对象
* @return bean对象
* @throws BeansException 异常
*/
public static T getBean(String name) throws BeansException {
if (context == null) {
return null;
}
return (T) context.getBean(name);
}
/**
* 根据bean类型获取bean
*
* @param clazz 类型
* @param 返回对象
* @return bean对象
* @throws BeansException 异常
*/
public static T getBean(Class clazz) {
if (clazz == null) return null;
return context.getBean(clazz);
}
public static ApplicationContext getContext() {
if (context == null) return null;
return context;
}
}