普通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");

猜你喜欢

转载自blog.csdn.net/LittleFlown/article/details/79224747
今日推荐