Results 1 to 4 of 4

Thread: how to reference an outside object in IoC

  1. #1
    Join Date
    Aug 2005
    Posts
    8

    Default how to reference an outside object in IoC

    //MockView.java
    Code:
    public class MockView extends ViewPart {
    
        public void createPartControl(Composite parent) {		       
    		Button btn = new Button(parent, SWT.PUSH);
        }
    
    }
    I want to config the btn Button as below:

    //beans.xml
    Code:
    <bean id="btn" class="org.eclipse.swt.widgets.Button">
        <constructor-arg>
            <!-- How to config the parent object created out of this file -->
        </constructor-arg>
    
        <constructor-arg>
            <!-- How to config the static variable of org.eclipse.swt.SWT.PUSH -->
        </constructor-arg>
    </bean>
    How to config the parent object which not exists in beans.xml and the static vairable of SWT.PUSH ?

    Thanks in advance!

  2. #2
    Join Date
    Sep 2005
    Location
    João Pessoa, Paraíba - Brazil
    Posts
    5

    Default

    If it's a static variable you can't get it using the bean factory. Wrap it using a factory bean and then return the static object.

    You can find more about factory beans here: FactoryBean
    Coding: At the end of the day, if the program doesn\'t run and make money for the client, you haven\'t done anything.

  3. #3
    Join Date
    Aug 2005
    Posts
    8

    Default

    Thank you for your advice, but is it advisable of creating a new object in order to ref a static variable?

    and the other problem is difficult for, and no any information in the document,

  4. #4
    Join Date
    Sep 2005
    Location
    João Pessoa, Paraíba - Brazil
    Posts
    5

    Default

    If the static variable is just a "singleton" it shoudn't even exist, 'cos Spring can handle singletons natively.

    Do you really need a static variable? Why can't you just declare it as a singleton bean in Spring?

    A simple factory bean implementation for your problem:

    Code:
    public class Factory implements FactoryBean &#123;
    
        public boolean isSingleton&#40;&#41; &#123;
            return true;
        &#125;
    
        public Class getObjectType&#40;&#41; &#123;
           return SWT.PUSH.getClass&#40;&#41;;
        &#125;
    
        public Object getObject&#40;&#41; &#123;
            return SWT.PUSH;
        &#125;
    
    &#125;
    Now you just need to use this bean as a reference to the static variable in your bean factory config.
    Coding: At the end of the day, if the program doesn\'t run and make money for the client, you haven\'t done anything.

Similar Threads

  1. Replies: 2
    Last Post: Oct 10th, 2005, 05:12 PM
  2. Spring container fails with no exception
    By naor in forum Container
    Replies: 9
    Last Post: Oct 1st, 2005, 03:39 PM
  3. EHCaching Hibernate
    By dencamel in forum Data
    Replies: 3
    Last Post: Sep 6th, 2005, 09:03 PM
  4. Loosing my SecureContext
    By sklakken in forum Security
    Replies: 3
    Last Post: Jul 21st, 2005, 01:44 PM
  5. Stack Overflow
    By rayho222 in forum Container
    Replies: 6
    Last Post: May 17th, 2005, 03:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •