SSH加载顺序问题

时间:2023-03-10 00:38:39
SSH加载顺序问题

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;
    }
}