当前位置: 代码迷 >> 综合 >> spring工具类 获取bean
  详细解决方案

spring工具类 获取bean

热度:81   发布时间:2023-11-03 07:46:59.0

工具类:

public class SpringBeanFactoryUtils implements ApplicationContextAware {private static ApplicationContext appCtx;public static ApplicationContext getApplicationContext() {return appCtx;}@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {appCtx = applicationContext;}public static Object getBean(String beanName) {return appCtx.getBean(beanName);}
}

注入spring: 

<bean id="springBeanFactoryUtils" class="test.utils.SpringBeanFactoryUtils"/>

获取bean:

TestService testService = (TestService ) SpringBeanFactoryUtils.getBean("testService Impl");

 

 

  相关解决方案