要获取各种bean,需要用到 ApplicationContext
获取它的引用方式是先绑定它:
@Component
public class AppContext implements ApplicationContextAware
{
private static ApplicationContext context;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
{
context = applicationContext;
}
public static ApplicationContext getContext()
{
return context;
}
}
然后通过此类型的静态方法 getContext 获取。