Spring IOC实例化了哪些对象

Spring IOC实例化了哪些对象



启动servlet容器(tomcat),加载web.xml-->加载applicationContext.xml
spring的配置文件 applicationContext.xml大致如下:

   
    //将beanService对象注入到Action对象 也可以用注解@Resource beanService
    <bean id="Login" class="sshdemo.func001.action.LoginAction"  scope="prototype">
        <property name="personService" ref="personService" />
    </bean>

    //将Dao对象注入到beanService对象里面
    <bean id="personService" class="sshdemo.func001.service.PersonServiceImpl">
        <property name="personDAO" ref="PersonDAO" />
    </bean>

    //将HibernateTemplate注入到Dao对象里面
    <bean id="PersonDAO" class="sshdemo.func001.dao.PersonDAOImpl">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    //将DB连接设置到SessionFactory 也可以通过加载hibernate.cfg.xml创建SessionFactory对象
    <bean id="sessionFactory"         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
    </bean>  

    //创建DB连接,也可以通过配置文件hibernate.cfg.xml里配置参数
    <bean id="dataSource"
        class="org.apache.commons.dbcp.BasicDataSource">
     </bean>



然后在启动tomcat之后,还没有运行程序前,自己配置的log4j打出的日志有如下:
---------------------------------日志分割线--------------------------------------
Loaded 6 bean definitions from location pattern [ /WEB-INF/applicationContext.xml]
。。。
6 beans defined in org.springframework.web.context.support.XmlWebApplicationContext@179dce4: display name [Root WebApplicationContext]; startup date [Thu May 14 08:44:00 CST 2009]; root of context hierarchy
。。。
Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@6210fb: defining beans [ Login,personService,PersonDAO,sessionFactory,dataSource,ChartAction]; root of factory hierarchy
Creating shared instance of singleton bean 'personService'
Creating instance of bean 'personService'
Eagerly caching bean 'personService' to allow for resolving potential circular references
Creating shared instance of singleton bean 'PersonDAO'
Creating instance of bean 'PersonDAO'
Eagerly caching bean 'PersonDAO' to allow for resolving potential circular references
Creating shared instance of singleton bean 'sessionFactory'
Creating instance of bean 'sessionFactory'
Eagerly caching bean 'sessionFactory' to allow for resolving potential circular references
Creating shared instance of singleton bean 'dataSource'
Creating instance of bean 'dataSource'
Eagerly caching bean 'dataSource' to allow for resolving potential circular references
。。。
---------------------------------日志分割线--------------------------------------

在启动servlet容器(tomcat)时,
1.spring就已经初始化了action,PersonServiceImpl,PersonDAOImpl,sessionFactory,dataSource
2.struts2的具体action实例也实例化,但是没包装数据,当用户访问*.action时,会把jsp页面里面的参数包装设置到action的属性里面。这里与struts 1.x不同之处:struts是*.do,经过web.xml过滤,查找有没有请求的action,有--->通过struts-config.xml-->找到<action../>-->对应的beanForm-->把请求参数设置到beanForm里面去。

猜你喜欢

转载自ewf-momo.iteye.com/blog/1708665