标签:over frame 返回 throws logs one imp ext stat
随便一百度,网上一大把,并且还不止一种。所以这里就只记录目前用的一种好了。
实现ApplicationContextAware接口 即可:
import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; @Component public class SpringUtils implements ApplicationContextAware { private static ApplicationContext applicationContext = null; @Override public void setApplicationContext(ApplicationContext arg0) throws BeansException { if (SpringUtils.applicationContext == null) { SpringUtils.applicationContext = arg0; } } // 获取applicationContext public static ApplicationContext getApplicationContext() { return applicationContext; } // 通过name获取 Bean. public static Object getBean(String name) { return getApplicationContext().getBean(name); } // 通过class获取Bean. public static <T> T getBean(Class<T> clazz) { return getApplicationContext().getBean(clazz); } // 通过name,以及Clazz返回指定的Bean public static <T> T getBean(String name, Class<T> clazz) { return getApplicationContext().getBean(name, clazz); } }
使用时,直接SpringUtils.getBean()即可。
标签:over frame 返回 throws logs one imp ext stat
原文地址:http://www.cnblogs.com/hyyq/p/7003055.html