applicationContext是ApplicaionContext实例
如何配置可以在服务器开始时就自动加载spring配置文件,并实例里面的bean 
而不需要 ApplicionContext applicationContext = new ClasspathApplicionContext(“..”) 
            applicationContext.getBean("..");

解决方案 »

  1.   

    在web.xml里面配置
    <listener>
       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      <context-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>/WEB-INF/applicationContext.xml</param-value>
      </context-param>
    不知道是不是你说的那样
      

  2.   

    第一个Bean
    package com.bean;public class User {
    private String name;
    private String pass;
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    public String getPass() {
    return pass;
    }
    public void setPass(String pass) {
    this.pass = pass;
    }
    public void print(){
    System.out.print("av");
    }
    }第二个Bean public class LoginAction extends Action {
    private User user;
    public void setUser(User user) {
    this.user = user;
    }

    public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) {
    user.print();
    return null;
    }
    }
    applicationContext.xml:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

    <bean id="user"
    class="com.bean.User">
    <property name="name"
    value="123">
    </property>
    <property name="pass"  value="123">
    </property>
    </bean>
    <bean id ="loginAction" class="com.yourcompany.struts.action.LoginAction">
      <property name="user" ref="user"></property>
    </bean>

    </beans>
    web.xml:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
      <servlet>
        <servlet-name>action</servlet-name>
        <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
        <init-param>
          <param-name>config</param-name>
          <param-value>/WEB-INF/struts-config.xml</param-value>
        </init-param>
        <init-param>
          <param-name>debug</param-name>
          <param-value>3</param-value>
        </init-param>
        <init-param>
          <param-name>detail</param-name>
          <param-value>3</param-value>
        </init-param>
        <load-on-startup>0</load-on-startup>
      </servlet>
      
      <servlet-mapping>
        <servlet-name>action</servlet-name>
        <url-pattern>*.do</url-pattern>
      </servlet-mapping>
      
      <listener>  
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
      </listener> 
      
       <context-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>classpath*:applicationContext.xml</param-value>
      </context-param>
      
       
     
      
      
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
    </web-app>
      

  3.   

    上面是大致的框架,会出现null pointer错误啊
      

  4.   

    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext*.xml</param-value>
    </context-param>
      

  5.   

    这种都是必须的啊,谁做web开发不在 web.xml里面配??楼主,你既然要用 spring,就必须按照他的BeanFactory方式参数Bean。他只是帮你new出来而已,和你自己new没有任何区别。不过他能帮你管理。如果你不用 spring,那把类定义成,除非你把类定义成 静态类,这样加载时就不用初始化。不过静态类一般都是内部类。
      

  6.   

    可以自己写个基类 然后子类继承它
       protected static Object getBean(ServletContext servletContext,String beanName){
       WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(servletContext);
       return wac.getBean(beanName);
       }
    servletContext这个你因该在你的web项目中能得到的 这样就很方便的直接返回你要的bean了 HOHO~