feat: 增加spring上下文
This commit is contained in:
parent
3566c9facf
commit
6036548f45
@ -0,0 +1,40 @@
|
|||||||
|
package com.flyfish.framework.context;
|
||||||
|
|
||||||
|
import org.springframework.beans.BeansException;
|
||||||
|
import org.springframework.beans.factory.BeanInitializationException;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.ApplicationContextAware;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* spring的上下文
|
||||||
|
*
|
||||||
|
* @author wangyu
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class SpringContext implements ApplicationContextAware {
|
||||||
|
|
||||||
|
private ApplicationContext applicationContext;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置上下文
|
||||||
|
*
|
||||||
|
* @see BeanInitializationException
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||||
|
this.applicationContext = applicationContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过类获取某个bean
|
||||||
|
* 优先获取@Prmary注解的bean
|
||||||
|
*
|
||||||
|
* @param clazz 类
|
||||||
|
* @param <T> 泛型
|
||||||
|
* @return 结果
|
||||||
|
*/
|
||||||
|
public <T> T getBean(Class<T> clazz) {
|
||||||
|
return applicationContext.getBean(clazz);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user