Java获取spring注解bean

获取注释方式(Annotaction)的bean

   
WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(config.getServletContext());   
    TestService testService= (TestService) wac.getBean("TestService");  


获取ApplicationContext.xml中的bean

   
BeanFactory factory = new ClassPathXmlApplicationContext("classpath:/applicationContext*.xml");  
              
    ITestService testService = (ITestService)factory.getBean("test-service"); 

你可能感兴趣的:(spring注解)