spring何时调用applicationcontext.xml文件?放在容器里,我是这样理解的tomcat作为servlet的容器,会在web.xml里加载servlet,
在web.xml里这样:
                         "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

<servlet>
  <servlet-name>SpringContextServlet</servlet-name>
  <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>   
  <load-on-startup>1</load-on-startup>
</servlet>  <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>
  <load-on-startup>1</load-on-startup>
 </servlet>.....
下面的省略了.
applicationcontext.xml 和web.xml 都放在WEB-INF目录下
ContextLoaderServlet这个类不再靠"依赖注入"方法,而是靠另一种spring的核心技术叫什么servcie locator (服务定位器)
就是说可以写一个BaseAction(这个action也是继承了struts的action)
然后在这个里面
public class BaseAction extends Action { private IOrderService orderService; public void setServlet(ActionServlet actionServlet) {
super.setServlet(actionServlet);
ServletContext servletContext = actionServlet.getServletContext();
WebApplicationContext wac =
WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
this.orderService = (IOrderService) wac.getBean("orderService");
} protected IOrderService getOrderService() {
return orderService;
}}然后你的所有的action都要继承这个BaseAction,通过getOrderSevvice()获得DAO,来实现对底层数据库的操作 ...这个接口是我自己写的,你要根据你自己的需求写.晕,不知道我讲清楚类没有.

解决方案 »

  1.   

    在你现在的这个程序里面,要加一些代码:
    public static void main(String a[])
    {
    Test t= new Test();
    List list=t.selectUser();
    System.out.print(list.size());
    }
    改成
    public static void main(String a[])
    {
    Test t= new Test();
                      ApplicationContext ctx = new FileSystemXmlApplicationContext(
                    "applicationtext.xml");
                      t.setSessionFactory((SessionFactory)ctx.getBean("sessionFactory"));
    List list=t.selectUser();
    System.out.print(list.size());
    }
      

  2.   

    上面的问题搞定了,不过,又产生了下一个问题,呵呵:问题是这样的:我在配置文件中定义了一个bean,名为"test",那么我在jsp中怎么引用这个bean呢?我是这样写的<%test.get***()%>可这样就报错了,后来把Test类的方法get***()改为static的,能解决问题,可以得到注射进去的值,不过总感觉不对,应该怎样才能使用getBean()方法来获取Bean呢?
      

  3.   

    上面的Test是一个类,有一个String数据成员 通过注射设定值,然后在JSP文件中显示,没有任何接口。
      

  4.   

    WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(application);
    Test test = (Test)applicationContext.getBean("test");
    test.getBean();
    前提是要在web.xml配置:
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param><servlet>
      <servlet-name>SpringContextServlet</servlet-name>
      <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>  
      <load-on-startup>1</load-on-startup>
    </servlet>