Announcement Announcement Module
Collapse
No announcement yet.
How spring managaes multiples beans defined Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How spring managaes multiples beans defined

    Hi, I'm a newbie in springs and I was trying to set up an application with it, I have read some docs and a basic book about it.

    Now I have the configuration below, which has many beans of the same clases (like 3 of Publisher and 3 of CoreDashboardController) then the only difference between them is that they hold a reference to a different bean each one, each CoreDashboardControllers has a reference to one of the Publishers, but this are different because each one holds a reference to a different Identity. In this parts my questions are:

    in general, how you would do it?
    this configuration will hold instances of the 3 different Publisher and 3 different CoreDashboardController always, or depending of what is being requested?

    The other part, which I will try to see if works or not today, so I just have the curiosity is the URL managment. What I try to achieve there is that with a single Controller (MultiActionController) handle 6 different url, but this controller only has two methods, what I try to achieve with the beans refences explained above is thah this controller morphs depending on the request.

    Regards and thanks in advance.

    Code:
      <bean name="PersistanceManager" class="com.foo.persistance.Manager">
        <property name="sessionFactory">
          <ref local="sessionFactory"/>
        </property>
      </bean>
    
      <!-- Business Logic class: Publisher -->
      <bean name="PipCore" class="com.foo.core.Publisher">
        <property name="pi">PersistanceManager</property>
        <property name="source">IdentityFromConfig</property>
      </bean>
    
      <bean name="PipCoreIp" class="com.foo.core.Publisher">
        <property name="pi">PersistanceManager</property>
        <property name="source">IdentityIP</property>
      </bean>
    
      <bean name="PipCoreContact" class="com.foo.core.Publisher">
        <property name="pi">PersistanceManager</property>
        <property name="source">IdentityContact</property>
      </bean>
    
      <bean name="IdentityIP" class="com.foo.core.IdentityIP"/>
      <bean name="IdentityContact" class="com.foo.core.IdentityContact"/>
      <bean name="IdentityFromConfig" class="com.foo.core.IdentityFromConfig"/>
    
      <!-- MVC Controllers -->
      <bean id="CoreDashboardController"
    	class="com.foo.web.CoreDashboardController">
        <property name="methodNameResolver" ref="CoreDashboardControllerMethodNameResolver"/>
        <property name="core"><ref bean="PipCore"/></property>
      </bean>
    
      <bean id="CoreDashboardControllerIp" 
    	lass="com.foo.web.CoreDashboardController">
        <property name="methodNameResolver" ref="CoreDashboardControllerMethodNameResolver"/>
        <property name="core"><ref bean="PipCoreIp"/></property>
    
      </bean>
    
      <bean id="CoreDashboardControllerContact"
    	class="com.foo.web.CoreDashboardController">
        <property name="methodNameResolver" ref="CoreDashboardControllerMethodNameResolver"/>
        <property name="core"><ref bean="PipCoreContact"/></property>
      </bean>
    
    
      <!-- MethodNameResolver for dashboard -->
      <bean id="CoreDashboardControllerMethodNameResolver"
    	class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
        <property name="mappings">
          <props>
    	<prop key="/core/*/Activity">Activity</prop>
    	<prop key="/core/*/TopActivity">TopActivity</prop>
          </props>
        </property>
      </bean>
    
      <!-- Vistas -->
      <bean id="urlMapping"
    	class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
          <props>
    	<prop key="/core/Ip/Activity">CoreDashboardControllerIp</prop>
    	<prop key="/core/Ip/TopActivity">CoreDashboardControllerIp</prop>
    	<prop key="/core/Contact/Activity">CoreDashboardControllerContact</prop>
    	<prop key="/core/Contact/TopActivity">CoreDashboardControllerContact</prop>
    	<prop key="/core/fromConfig/Activity">CoreDashboardController</prop>
    	<prop key="/core/fromConfig/TopActivity">CoreDashboardController</prop>
          </props>
        </property>
      </bean>

  • #2
    Every time you declare a bean, you are telling spring to create a new instance. If this is set to the default scope, e.g. singleton then you'll only ever create one per container. I would recommend having a read of the reference manual.
    http://www.springframework.org/docs/...factory-scopes

    Comment


    • #3
      The instance can be either singleton or prototype but there is always one and only one BeanDefinition to be shared for all the request to bean. I 'm actually new user of Spring, but i think that what in background is merely the reflection.

      Comment


      • #4
        Originally posted by hoanglovely View Post
        The instance can be either singleton or prototype but there is always one and only one BeanDefinition to be shared for all the request to bean. I 'm actually new user of Spring, but i think that what in background is merely the reflection.
        I'm not sure I understand your point.

        Comment

        Working...
        X