一、接口介紹 二、接口使用 1.編寫工具類 1 import org.springframework.beans.BeansException; 2 import org.springframework.context.ApplicationContext; 3 import org.springframework.context.ApplicationContextAware; 4 /** 5 * Created by zl on 2018/7/7. 6 */ 7 public class BeanFactoryUtil implements ApplicationContextAware { 8 protected static ApplicationContext ctx = null; 9 10 @Override11 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {12 ctx = applicationContext;13 }14 15 public static Object getBean(String beanId) {16 return ctx.getBean(beanId);17 }18 } 2.在applicationContext.xml中注冊BeanFactoryUtil工具類 <bean id="beanFactoryUtil" class="com.boss.utils.BeanFactoryUtil"/> 3.測試 |
|