http://bbs.csdn.net/topics/390299835

个人总结

1.项目启动首先加载WEB.xml文件

wen.xml文件中有

    <!-- tomcat默认生成的地方是classes下面 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:applicationContext*.xml</param-value>
    </context-param>

2.继而去加载所有的applicationContext*.xml文件

在applicationContext*.xml文件中有

<!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效 -->   
     <ehcache:annotation-driven cache-manager="cacheManager"/>
     
    <!-- Configurer that replaces ${...} placeholders with values from properties files -->
    <!-- application.properties文件是关于数据库链接、配置内容-->
    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:application.properties</value>
            </list>
        </property>
    </bean>

<!-- cacheManager工厂类,指定ehcache.xml的位置 -->
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <property name ="configLocation" >
             <value>classpath:ehcache.xml</value>
        </property>
    </bean>

网上找的资料:(非常棒的阐述,大家好好研究)

先是Struts1.x 与Spring的结合配置使用流程:
该方案通过"自动注入"的方式获取对象,也即"依赖注入"方式。
较为常用,但特定情况下使用不了。如两个JVM,一个在长沙运行,一个在深圳运行,就不能同时实现注入,(1)"Spring和Struts的支持环境构建"
复制Struts、Spring相关包,并提供Struts、Spring的配置
提供国际化支持,提供缺省的国际化资源文件。
(2)"配置web.xml"
配置<context-param>,其中内容为Spring的配置文件applicationContext.xml。注意<param-name>的内容,必须为"contextConfigLocation";
配置<listener>,使用Spring提供的ContextLoaderListener。
配置范例:

XML/HTML code?
1
2
3
4
5
6
7
<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>



解析:
当Web应用启动时,将执行以下操作:
①由Tomcat创建一个ServletContext,并自动载入<context-param>中的属性;
②ContextLoaderListener检测到ServletContext的创建,运行Spring的相关程序;
③Spring根据<context-param>中contextConfigLocation的指向,加载对应applicationContext.xml;
④Spring根据applicationContext.xml的内容创建一个BeanFactory实例,并放入ServletContext中。
简而言之,该配置的作用是:当Web应用启动时,Spring将自动创建一个BeanFactory实例,并放入ServletContext中。
(3)"配置Struts"
struts-config.xml文件中,注意其<action>标签的type属性,设置为Spring的代理Action类。由此真正的action创建交给Spring完成。
若不使用Spring,type设置为com.project.LoginAction等自定义类;使用Spring,type设置为"org.springframework.web.struts.DelegatingActionProxy"。
但"仍需构建具体Action",并编写Action的具体方法。该Action"在Spring的配置中引用",详见"(4)配置Spring"。
相当于将Struts配置转移到了Spring配置中,此处即Struts与Spring框架结合的关键结合点。
配置范例:

XML/HTML code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<struts-config>
    <form-beans>
        <form-bean name="loginForm" type="com.project.usermgr.web.forms.LoginActionForm"/>
    </form-beans>
    <action-mappings>
        <action path="/login"
                type="org.springframework.web.struts.DelegatingActionProxy"
                name="loginForm"
                scope="request"
        >
            <forward name="success" path="/login_success.jsp"/>
        </action>
    </action-mappings>
    <message-resources parameter="MessageResources" />
</struts-config>



(4)"配置Spring"
由Spring来创建和管理Action,并向Action注入Model层Service对象。
设置scope="prototype"后可使每个线程都有一个新的Action,从而解决Struts1.x的Action线程安全问题。
注意:
①必须使用name属性,且name属性的值必须和struts-config.xml文件中<action>标签的path属性值一致
②必须配置Model层Service对象,如userManager等。
配置范例:

XML/HTML code?
1
2
3
4
applicationContext-beans.xml:
    <bean name="/login" class="com.project.usermgr.web.actions.LoginAction" scope="prototype">
        <property name="userManager" ref="userManager"/>
    </bean>



(5)"构建Action"
Model层Service对象由Spring"自动注入",因此无需手动构建该对象,也无需获取BeanFactory。通过"自动注入"的方式获取对象,即"依赖注入"。
注意,相关对象须提供set方法,以方便Spring注入。

Java code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class LoginAction extends Action {
    private UserManager userManager;
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        LoginActionForm laf = (LoginActionForm)form;
        String username = laf.getUsername();
        String password = laf.getPassword();
        userManager.login(username, password);
        return mapping.findForward("success");
    }
    //须提供set方法,以方便Spring注入。
    public void setUserManager(UserManager userManager) {
        this.userManager = userManager;
    }
}

SSH加载顺序问题的更多相关文章

  1. linux下/etc/profile /etc/bashrc /root/.bashrc /root/.bash_profile这四个配置文件的加载顺序

    目录 一.关于linux配置文件 二.验证四个配置文件的加载顺序 三.结论 一.关于linux配置文件 1.linux下主要有四个配置文件:/etc/profile ./etc/bashrc ./ro ...

  2. web.xml加载顺序

    一 1.启动一个WEB项目的时候,WEB容器会去读取它的配置文件web.xml,读取<listener>和<context-param>两个结点. 2.紧急着,容创建一个Ser ...

  3. web.xml文件加载顺序

    1.启动一个WEB项目的时候,WEB容器会去读取它的配置文件web.xml,读取<listener>和<context-param>两个结点. 2.紧急着,容创建一个Servl ...

  4. web.xml 中的listener、 filter、servlet 加载顺序及其详解

    在项目中总会遇到一些关于加载的优先级问题,近期也同样遇到过类似的,所以自己查找资料总结了下,下面有些是转载其他人的,毕竟人家写的不错,自己也就不重复造轮子了,只是略加点了自己的修饰. 首先可以肯定的是 ...

  5. css样式加载顺序及覆盖顺序深入理解

    注:内容转载 很多的新手朋友们对css样式加载顺序和覆盖顺序的理解有所偏差,下面用示例为大家详细的介绍下,感兴趣的朋友不要错过 { height: 100%; width: 200; position ...

  6. Java---类加载机制,构造方法,静态变量,(静态)代码块,父类,变量加载顺序

    直接上代码: 代码1: public class ConstroctTest { private static ConstroctTest test = new ConstroctTest(); // ...

  7. DOM加载顺序

    最近一直在困扰dom的加载顺序问题,经常会遇到以为绑定好的事件不响应等情况,一头雾水,直到请教了周围的同事,才发现了解dom的加载顺序是多么的重要. 关于这个问题,其实网上已经有一些介绍,但是我觉得并 ...

  8. PHP 依赖注入,从此不再考虑加载顺序

    说这个话题之前先讲一个比较高端的思想--'依赖倒置原则' "依赖倒置是一种软件设计思想,在传统软件中,上层代码依赖于下层代码,当下层代码有所改动时,上层代码也要相应进行改动,因此维护成本较高 ...

  9. MVC中 _ViewStart _Layout Index三个页面中的加载顺序

    MVC学习中忽然想到一个问题.. 在访问一个Index.cshtml页面时, MVC的加载顺序是怎么样的呢? 首先说下我的结论 . _ViewStart.cshtml . Index.cshtml . ...

随机推荐

  1. 使用powerdesigner创建数据库表

    (1 )新建概念模型 (2 )新建表,添加表各个属性 填写属性名称和类型,主键要勾选上P,M,D. (3) 如何各个表中有相同的字段名,需要设置Tool->Model Options,把红色区域 ...

  2. winform自定义日期控件,要求可以手动输入日期DatePicker

    要求:文本框中能手动输入数字,向上箭头根据鼠标位置给年月日递增,向下箭头递减 一:页面加载时: private void FlatDatePicker_Load(object sender, Even ...

  3. jdk源码分析之ArrayList

    ArrayList关键属性分析 ArrayList采用Object数组来存储数据 /** * The array buffer into which the elements of the Array ...

  4. C#的变迁史 - C# 5.0 之调用信息增强篇

    Caller Information CallerInformation是一个简单的新特性,包括三个新引入的Attribute,使用它们可以用来获取方法调用者的信息, 这三个Attribute在Sys ...

  5. C#的变迁史 - C# 5.0 之并行编程总结篇

    C# 5.0 搭载于.NET 4.5和VS2012之上. 同步操作既简单又方便,我们平时都用它.但是对于某些情况,使用同步代码会严重影响程序的可响应性,通常来说就是影响程序性能.这些情况下,我们通常是 ...

  6. MySQL远程登录设置

    可以在一台机器上访问另一台机器的MySQL,但是需要一些设置. 进入MySQL后,输入以下命令: GRANT ALL PRIVILEGES ON *.* TO 'tigase'@'%' IDENTIF ...

  7. c语言是如何实现泛型链表

    最近有看一点Linux内核源码,发现内核里大量使用了list_head结构体.百度查了一下,原来内核利用这个结构体实现了泛型. 自认为对链表已经很熟悉的我,决定自己实现一下. 下面以Node和list ...

  8. 2016 长春东北赛---Coconuts(离散化+DFS)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5925 Problem Description TanBig, a friend of Mr. Frog ...

  9. HDU 5510---Bazinga(指针模拟)

    题目链接 http://acm.hdu.edu.cn/search.php?action=listproblem Problem Description Ladies and gentlemen, p ...

  10. 读书笔记系列之java性能优化权威指南 一 第一章

    主题:java性能优化权威指南 pdf 版本:英文版 Java Performance Tuning 忽略:(0~24页)Performance+Acknowledge 1.Strategies, A ...