Announcement Announcement Module
Collapse
No announcement yet.
newbie question about the BeanFactory Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • newbie question about the BeanFactory

    Hi all,

    I'm experimenting with Spring to find out what it is and what in can do for me. I created a simple definition with 2 singletons of the same object:

    <beans>
    <bean name="a"
    class="com.company.spring.ProjectImpl"
    singleton="true">
    <constructor-arg>
    <value>Hello world a!</value>
    </constructor-arg>
    </bean>
    <bean name="b"
    class="com.company.spring.ProjectImpl"
    singleton="true">
    <constructor-arg>
    <value>Hello world b!</value>
    </constructor-arg>
    </bean>
    </beans>

    My question is what do I have to do when I do not know beforehand how many of those singletons I need because I need to determine it programmatically? Can I control the BeanFactory programmatically?
    I need singletons because there are multiple projects but the ProjectImpl objects are shared among different users

    Could someone please help me on the way?

    Thanks and regards,

    Jeroen

  • #2
    Re: newbie question about the BeanFactory

    Originally posted by jeroenverhagen
    Hi all,

    I'm experimenting with Spring to find out what it is and what in can do for me. I created a simple definition with 2 singletons of the same object:

    <beans>
    <bean name="a"
    class="com.company.spring.ProjectImpl"
    singleton="true">
    <constructor-arg>
    <value>Hello world a!</value>
    </constructor-arg>
    </bean>
    <bean name="b"
    class="com.company.spring.ProjectImpl"
    singleton="true">
    <constructor-arg>
    <value>Hello world b!</value>
    </constructor-arg>
    </bean>
    </beans>

    My question is what do I have to do when I do not know beforehand how many of those singletons I need because I need to determine it programmatically?
    Maybe it would be better if those beans aren`t singletons. Evertime you retrieve one you get a new instance.

    If I need to create beans, I make a Factory:

    Code:
    class ProjectFactory&#123;
         private String _baseName;
         private AtomicInteger _counter = new AtomicInteger&#40;0&#41;;
    
         public ProjectFactory&#40;String baseName&#41;&#123;
                _baseName = baseName;
         &#125;
    
         public Project create&#40;&#41;&#123;
               return new ProjectImpl&#40;_baseName+_counter.incAndGet&#40;&#41;&#41;;
         &#125;
    &#125;
    And create the factory in Spring:
    Code:
    <bean "projectFactory" class="ProjectFactory">
         <constructor-arg index="0" value="Hello world"/>
    </bean>

    And if an objects need a Project, the ProjectFactory could be injected into it:
    Code:
    class Something&#123;
         private ProjectFactory _factory;
         
         Something&#40;ProjectFactory factory&#41;&#123;
            _factory = factory;
         &#125;
    
         void foo&#40;&#41;&#123;
               Project project = _factory.create&#40;&#41;;
        &#125;
    &#125;
    And you can write this one in Spring also:

    Code:
    <bean id="something" class="Something">
         <construtor-arg index="0" ref="projectFactory"/>
    </bean>

    Comment


    • #3
      Maybe it would be better if those beans aren`t singletons. Evertime you retrieve one you get a new instance.
      The reasons I would choose for singletons are:
      - the reference documentation advises it
      - each project object corresponds to one database with multiple users, I want the read-only parts of the project to be shared among users to save database access.

      If I need to create beans, I make a Factory:
      I'm confused, your code shows 'traditional' factories. What I gattered from documentation sofar is that the whole point of Spring is to use Spring as your bean factory?

      Thanks and regards,

      Jeroen

      Comment


      • #4
        Originally posted by jeroenverhagen
        The reasons I would choose for singletons are:
        - the reference documentation advises it
        How do you mean? Spring doesn`t say you need to make it a singleton.

        - each project object corresponds to one database with multiple users, I want the read-only parts of the project to be shared among users to save database access.
        Could you elaborate on this?

        If I need to create beans, I make a Factory:
        I'm confused, your code shows 'traditional' factories. What I gattered from documentation sofar is that the whole point of Spring is to use Spring as your bean factory?
        Not completely. You use Spring to glue your system together, but your system shouldn`t depend on Spring and no one should have access to the application context (almost no one).

        You shouldn`t abandon good programming practices and in this case you need a factory to create your projects. You can glue together the factory in Spring, but you give a reference to the factory to everyone that needs it. This way you don`t depend on Spring and it also makes testing a lot easier.

        Comment

        Working...
        X