| 1234567891011121314151617181920212223242526272829303132333435363738394041 |
- package com.dataeasy.server.utiis;
- import org.springframework.beans.BeansException;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.ApplicationContextAware;
- import org.springframework.stereotype.Component;
- /**
- * Spring上下文 工具类
- */
- @Component
- public class SpringUtils implements ApplicationContextAware {
- private static ApplicationContext applicationContext;
- @Override
- public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
- SpringUtils.applicationContext = applicationContext;
- }
- /**
- * 通过class获取Bean.
- *
- * @param clazz
- * @param <T>
- * @return
- */
- public static <T> T getBean(Class<T> clazz) {
- return applicationContext.getBean(clazz);
- }
- /**
- * 通过bean的名称获取对应的bean
- * @param beanName bean的名称
- * @return
- * @param <T>
- */
- public static <T> T getBean(String beanName) {
- return (T) applicationContext.getBean(beanName);
- }
- }
|