转载:tomcat+ssh 启动 加载的顺序。

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

<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,并自动载入中的属性;
②ContextLoaderListener检测到ServletContext的创建,运行Spring的相关程序;
③Spring根据中contextConfigLocation的指向,加载对应applicationContext.xml;
④Spring根据applicationContext.xml的内容创建一个BeanFactory实例,并放入ServletContext中。
简而言之,该配置的作用是:当Web应用启动时,Spring将自动创建一个BeanFactory实例,并放入ServletContext中。
(3)”配置Struts”
struts-config.xml文件中,注意其标签的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框架结合的关键结合点。
配置范例:

<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文件中标签的path属性值一致
②必须配置Model层Service对象,如userManager等。
配置范例:

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注入。

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

Struts2与Spring的结合就很简单了。
注意Struts2在web.xml中的配置:

<filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/ *</url-pattern>
</filter-mapping>

Struts2用的是一个filter。只有在用到action的时候,才会读取Struts2的配置,明显Spring先启动。

楼主的问题,问到Spring的构建机制和注入机制,其实很简单。
Spring由listener启动之后,读取Spring的配置文件applicationContext.xml。如有多个配置文件,会全部读取。注意,是“全部”读取,不管有多少个。
Spring的配置文件中,基本都是,一个bean对应一个对象。Spring在读取配置完毕后,就立刻实例化这些对象。所以,这些对象是很早就被实例化好了的,并被保存起来。当程序需要使用这些对象时,则直接从内存中获取。Spring会把这些实例好了的对象保存在一个map里,第一次用的时候从map里取,查找速度是很快的。且查找一次之后,程序使用的就是引用了。

这里要稍微注意的就是scope=”prototype”的情况。
①scope=”singleton”,默认值。顾名思义,单例。
②scope=”prototype”,每次都会生成新的实例,从IoC容器中获得的对象每次都是”不同”的。
若使用非线程安全的类,可以考虑使用prototype。

同其他对象一样,prototype在一开始也会被实例化一个对象出来。当此对象被使用后,Spring将额外再生成一个对象放到map里,供其他线程使用。

此外,Spring还有AOP机制。AOP说白了就是代理。一种是java的动态代理,还有一种则是CGLIB的继承式代理。这些代理类,与那些ioc容器的对象一样,也都是在Spring配置读取完成之后立刻被创建的。

Spring配置中的东西,都是一开始就被创建好了的,根本就不存在“重复查找”的问题。
看到个帖子,从Spring的源代码的角度分析了Spring的运行机制:
http://graykeel.iteye.com/blog/696984

摘抄结语部分的内容如下:

整个bean的生成过程:
1:生成xml格式的配置文件
2:通过ResourceLoader将配置文件读入
3:通过BeanDefinitionReader将配置信息转换程BeanDefinition
4:在通过BeanFactory根据BeanDefinition信息创建bean实例
5:将bean实例放入SingletonBeanRegistry中以供使用

发布了28 篇原创文章 · 获赞 5 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/java_utf8/article/details/72614900