小弟初学Spring,有一个问题纠结了很久没解决,废话不多说,进入正题
我配置好了Spring的XML文件:applicationContext.xml,注入一切都是正常的。但是具体调用那些bean的时候,我要是用
BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) factory.getBean("userService");
userService.getPassWordByUserName("wewe");
这样的方法完全可以,但是问题是。我现在要启动了tomcat,让bean在tomcat中创建好了,我该怎么调用那些bean?
不要用BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");这种方法了,这种方法只会再次加载一遍applicationContext.xml。我要调用tomcat中已经创建的现成的bean。
求指教!!

解决方案 »

  1.   

    在web.xml中加上这句
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
      <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    让tomcat 给你家在
      

  2.   

    在 web.config 文件里面配置什么东西 具体的我记不到了
      

  3.   

    你的意思应该是已经在web容器中初始化过spring, 然后想在诸如纯java类中调用bean? 可以用以下方式(spring的三种启动方式之一),不会重新初始化spring 的。
    ServletContext servletContext = this.getServletContext();   
    WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
    ***Bean bean = (***Bean)ctx.getBean("beanName");
      

  4.   

    web程序和桌面应用启动spring的方式不一样软件行业经典书籍
      

  5.   


    ServletContext servletContext = this.getServletContext(); 会报错,其中的this是什么?
      

  6.   

    我博客有SSH SSI 环境搭建和使用
      

  7.   

    貌似 LZ 不懂Spring的核心DI
      

  8.   

    this可以理解为一个指针,指向当前正在跑的那个。
    在Web.xml中配置:
      <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
     </context-param>
     
      <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
     </listener>
    WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext
    (this.getServletContext());
    Integer ing = (Integer) ctx.getBean("random");
    System.out.println(ctx.getBean("random"));
      

  9.   

    用监听器+WebApplicationContext ctx = WebApplicationContextUtils.get...
      

  10.   

    this.getServletContext()其中的this还是报错,不知道咋写。
      

  11.   

    各位能不能给一个带main方法的完整的例子。