1. WebApplicationContext wac = ContextLoader.getCurrentWebApplicationCon;
2. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();
3.
public class Springfactory implements BeanFactoryAware { private static BeanFactory beanFactory; // private static ApplicationContext context; public void setBeanFactory(BeanFactory factory) throws BeansException { this.beanFactory = factory; } /** * 根据beanName名字取得bean * * @param beanName * @return */ public static <T> T getBean(String beanName) { if (null != beanFactory) { return (T) beanFactory.getBean(beanName); } return null; } }
获得Spring容器
时间: 2024-10-16 02:51:50