You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by ren sky <by...@gmail.com> on 2009/05/14 04:03:16 UTC

struts2+spring+hibernate, objects instantiate order?

hello everyone,
i am new to struts2.
i use struts2, spring2.5 and hibernate 3.3.1 to build a simple website.
and have some questions about the order of the objects instantiate.
here is my config file for spring:

-------------------------------------applicationContext.xml------------------------------------------------
    <bean id="Login" class="sshdemo.func001.action.LoginAction"
scope="prototype">
        <property name="personService" ref="personService" />
    </bean>

    <bean id="personService" class="sshdemo.func001.service.PersonServiceImpl">
        <property name="personDAO" ref="PersonDAO" />
    </bean>

    <bean id="PersonDAO" class="sshdemo.func001.dao.PersonDAOImpl">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        .......
        .......
    </bean>

    <bean id="dataSource"
        class="org.apache.commons.dbcp.BasicDataSource">
        ........
        ........
    </bean>
-------------------------------------applicationContext.xml------------------------------------------------

and when i start the tomcat, before running my project,
i get the logs like this:

-----------------------------------------------log
contents------------------------------------------------
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
........
 -----------------------------------------------log
contents------------------------------------------------

does that mean
1) before running my project, spring has already instantiate all the
object defined in the applicationContext.xml?
   such as LoginAction,PersonServiceImpl and sessionFactory?
2) the action  used in struts2 is instantiated before a request comes
in? or is instantiated  when a real request comes in?
3) with spring,what is the role of struts2 ? it seems that the
responsibility of instantiate object is changed to spring, not
struts2.

thanks.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: struts2+spring+hibernate, objects instantiate order?

Posted by Jim Kiley <jh...@summa-tech.com>.
1) is correct.  I can't tell you the number of times that, on app startup,
I've gotten reams and reams of error messages because Spring couldn't
instantiate some bean or another.2) is both -- which you could determine for
yourself pretty easily by putting a logging statement in the action's
constructor.

jk

On Wed, May 13, 2009 at 10:03 PM, ren sky <by...@gmail.com> wrote:

> 1) before running my project, spring has already instantiate all the
> object defined in the applicationContext.xml?
>   such as LoginAction,PersonServiceImpl and sessionFactory?
> 2) the action  used in struts2 is instantiated before a request comes
> in? or is instantiated  when a real request comes in?
> 3) with spring,what is the role of struts2 ? it seems that the
> responsibility of instantiate object is changed to spring, not
> struts2.
>
> thanks.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>


-- 
Jim Kiley
Senior Technical Consultant | Summa
[p] 412.258.3346
http://www.summa-tech.com

Re: struts2+spring+hibernate, objects instantiate order?

Posted by Dave Newton <ne...@yahoo.com>.
ren sky wrote:
> 1) before running my project, spring has already instantiate all the
> object defined in the applicationContext.xml?
>    such as LoginAction,PersonServiceImpl and sessionFactory?

Singletons probably are, sure.

> 2) the action  used in struts2 is instantiated before a request comes
> in? or is instantiated  when a real request comes in?

Yes. IIRC Spring will try to instantiate objects on startup to make sure 
they can be instantiated as defined (I could be wrong about that).

Actions are definitely created per-request (assuming they're "prototype" 
scope, which they should be).

> 3) with spring,what is the role of struts2 ? it seems that the
> responsibility of instantiate object is changed to spring, not
> struts2.

Struts 2 is the web layer. You know, handling requests.

Dave


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org