Announcement Announcement Module
Collapse
No announcement yet.
Create bean at execution time Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Create bean at execution time

    Hi, it is possible to create beans at execution time?
    Thanks

    Leandro

  • #2
    If you explain what execution time means? How is it different to the normal way beans are created? Is lazy-init sufficient?

    Jörg

    Comment


    • #3
      The problem is that I need to create a JndiObjectFactoryBean depending on the existence of a system variable. The system variable indicates the jndiName. If this system variable exists, I want to create the JndiObjectFactoryBean with the jndiName. If not, I'll connect to the DB in another way and I don't want to create the JndiObjectFactoryBean.

      Regardless of that, I think creating beans at execution time is useful.
      Thanks!!

      Leandro

      Comment


      • #4
        I think a custom FactoryBean might do the trick. Basically pass it the beanname of the JndiObjectFactoryBean and the other way to connect to the DB (which I assume a javax.sql.DataSource of some kind). The FactoryBean internally do the logic. Something like the following:
        Code:
        public class ConditionalFactoryBean extends AbstractFactoryBean implements BeanFactoryAware {
            private String propertyName;
            private String conditionalObjectName;
            private String defaultObjectName;
            private BeanFactory beanFactory;
            protected Object createInstance() throws Exception {
                if (System.getProperty(propertyName) != null) {
                    return beanFactory.getBean(conditionalObjectName, getObjectType());
                } else {
                    return beanFactory.getBean(defaultObjectName, getObjectType());
                }
            }
        
            public Class getObjectType() {
                return javax.sql.DataSource.class;
            }
        
            public void setConditionalObjectName(String conditionalObjectName) {
                this.conditionalObjectName = conditionalObjectName;
            }
        
            public void setDefaultObjectName(String defaultObjectName) {
                this.defaultObjectName = defaultObjectName;
            }
        
            public void setPropertyName(String propertyName) {
                this.propertyName = propertyName;
            }
        
            public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
                this.beanFactory = beanFactory;
            }
        }
        To wire it up:
        Code:
        <bean id="jndiDataSource" lazy-init="true" class="org.springframework.jndi.JndiObjectFactoryBean">
          <property name="jndiName" value="${myProperty}"/>
          <!-- properties go here -->
        </bean>
        
        <bean id="otherDataSource" lazy-init="true" class="MyDataSource>
          <!-- properties go here -->
        </bean>
        
        <bean id="dataSource" class="ConditionalDataSource">
          <property name="propertyName" value="myProperty"/>
          <property name="conditionalObjectName"><idref local="jndiDataSource"/></property>
            <property name="defaultObjectName"><idref local="otherDataSource"/></property>
        </bean>
        Because the frist two datasources are lazy-init, they won't be created until one of them is retrieved in the creation of the dataSource bean.

        Comment


        • #5
          First of all, thanks for the clear and fast reply...

          I've try this, but you can't use ${myProperty} in the xml if myProperty isn't set. That is why I need to create the jndiDataSource at execution time

          Leandro

          Comment


          • #6
            That fails only if you don't ignore unresolvable placeholders. PropertyPlaceholderConfigurer has a property ignoreUnresolvablePlaceholders to change its behaviour.

            Jörg

            Comment


            • #7
              Ohh that's excellent, how can I change the ignoreUnresolvablePlaceholders attribute?
              I haven't defined any PropertyPlaceholderConfigurer, is there any default one??
              Thanks!!!!!

              Leandro

              Comment


              • #8
                Originally posted by leaqui View Post
                I haven't defined any PropertyPlaceholderConfigurer, is there any default one??
                Oh, I thought a PropertyPlaceholderConfigurer is the reason why the usage of ${myProperty} in XML fails for you:

                Originally posted by leaqui View Post
                I've try this, but you can't use ${myProperty} in the xml if myProperty isn't set.
                What does fail mean then? Do you have a stacktrace?

                And no, there should not be a default one.

                Originally posted by leaqui View Post
                Ohh that's excellent, how can I change the ignoreUnresolvablePlaceholders attribute?
                First have a look how to set up one. Second add the property ignoreUnresolvablePlaceholders with the value true.

                Jörg

                Comment


                • #9
                  I've found a hidden PropertyPlaceholderConfigurer.
                  Setting ignoreUnresolvablePlaceholders attribute works fine but I had to put
                  Code:
                  <property name="lookupOnStartup" value="false" />
                  <property name="proxyInterface" value="javax.sql.DataSource" />
                  at the jndiDataSource definition.
                  Thanks!!!

                  Leandro

                  Comment

                  Working...
                  X