对比新文件 |
| | |
| | | package com.gk.firework.Config.Oauth2; |
| | | |
| | | import org.springframework.beans.BeansException; |
| | | import org.springframework.context.ApplicationContext; |
| | | import org.springframework.context.ApplicationContextAware; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | /** |
| | | * Static injection of spring objects |
| | | * |
| | | * @author zhangby |
| | | * @date 2019-05-15 11:45 |
| | | */ |
| | | @Component |
| | | public class SpringContextUtil implements ApplicationContextAware { |
| | | |
| | | private static ApplicationContext applicationContext; |
| | | |
| | | /** |
| | | * 设置 setApplicationContext |
| | | * @param applicationContext application |
| | | * @throws BeansException beansException |
| | | */ |
| | | @Override |
| | | public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { |
| | | if (SpringContextUtil.applicationContext == null) { |
| | | SpringContextUtil.applicationContext = applicationContext; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取applicationContext |
| | | * @return ApplicationContext |
| | | */ |
| | | public static ApplicationContext getApplicationContext() { |
| | | return applicationContext; |
| | | } |
| | | |
| | | /** |
| | | * 通过name获取 Bean. |
| | | * @param name 名称 |
| | | * @return Object |
| | | */ |
| | | public static Object getBean(String name) { |
| | | return getApplicationContext().getBean(name); |
| | | } |
| | | |
| | | /** |
| | | * 通过class获取Bean. |
| | | * @param clazz class |
| | | * @param <T> T |
| | | * @return T |
| | | */ |
| | | public static <T> T getBean(Class<T> clazz) { |
| | | return getApplicationContext().getBean(clazz); |
| | | } |
| | | |
| | | /** |
| | | * 通过name,以及Clazz返回指定的Bean |
| | | * @param name name |
| | | * @param clazz class |
| | | * @param <T> T |
| | | * @return T |
| | | */ |
| | | public static <T> T getBean(String name, Class<T> clazz) { |
| | | return getApplicationContext().getBean(name, clazz); |
| | | } |
| | | |
| | | } |