1. 程式人生 > >普通java類中手動獲取spring管理的物件

普通java類中手動獲取spring管理的物件

1. 獲取ApplicationContext.xml中的bean

BeanFactory factory = new ClassPathXmlApplicationContext("classpath:/applicationContext.xml");
TestService testService = (TestService)factory.getBean("testService");


2. 獲取註解方式中的bean

WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();  
TestService testService = (TestService)wac.getBean("testService");