Servelt?你可以通过工厂拿到啊?Spring 一般都有一个工厂类来进行获取服务
比如
Factory.java
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Factory {
  private static BeanFactory beanFactory;
  static {
    try {
      beanFactory = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }  public static Object getBean(String beanName) {
    if (beanName == null) {
      return null;
    }
    try {
      Object obj = beanFactory.getBean(beanName);
      if (obj == null) {
        System.err.println("Lookup Object[" + beanName + "] Error!");
      }
      return obj;
    } catch (Exception ex) {
      System.out.println(beanName);
      ex.printStackTrace();
      return null;
    }
  }
}

解决方案 »

  1.   

    你在你的Spring的配置文件里设置好你的服务,然后通过MyService s = (MyService)Factory.getBean("MyServiceId"); 就可以拿到你的服务了!
      

  2.   

    既然app server启动时候初始化了spring的相关配置,再去手动创建工厂,获取bean就有点不划算了不如直接去ServletContext中获取,这样可以保证和struts中使用的bean是完全共享的public void doPost(HttpServletRequest request, HttpServletResponse response)  
        throws ServletException, IOException {  
        ServletContext application;  
        WebApplicationContext wac;  
        application = getServletContext();  
        wac = WebApplicationContextUtils.getWebApplicationContext(application);//获取spring的context  
        Service1 service1= (Service1) wac.getBean("service1");  
    }  
      

  3.   


    ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
    this.yourService = (YourService)ctx.getBean("yourService");