Spring环境下非注入类获取注入类的对象
程序员文章站
2022-05-25 22:56:35
...
继承ApplicationContextAware实现工具类:
@Component
public class SpringContextUtil implements ApplicationContextAware
{
private static ApplicationContext applicationContext = null;
@Override
public void setApplicationContext(ApplicationContext context)
throws BeansException
{
if (applicationContext == null)
{
applicationContext = context;
}
}
public static ApplicationContext getApplicationContext()
{
return applicationContext;
}
public static <T> T getBean(Class<T> clazz)
{
return getApplicationContext().getBean(clazz);
}
public static final Object getBean(String beanName)
{
return getApplicationContext().getBean(beanName);
}
}
在非Spring托管的类中获取Spring容器中托管的对象
上一篇: windows系统的右键新建文档消失解决
下一篇: 谈 utf8 文本中的BOM