Announcement Announcement Module
Collapse
No announcement yet.
Initializing beans based on system properties Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Initializing beans based on system properties

    Is there a way to initialize beans based on system properties so that based on system property you can cause different implementations to be instantiated?

  • #2
    With standard Spring, you can use PropertyPlaceholderConfigurers and ProperyOverrideConfigurers to override properties of beans. By default, the configurers use the system properties as fallback mechanism. There is no way currently that allows for changing an implementation of a bean based on system properties. You could develop your own BeanFactoryPostProcessor that allows for modification of the bean factory after it has been loaded (and thus change implementations). Have a look at the reference manual (ch. 3) to see how that works. The BeanFactory API docs should also help you a lot...

    I believe the AutoProxyCreators also replace beans after the context has been loaded (creates proxies for specific beans).

    Comment


    • #3
      Desired feature.....

      Basically what I would want is
      Code:
      <bean id="dataSource">
      <if property="environment" value="development">
      <class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName">
          <value>java&#58;comp/env/jdbc/OlbDataSource</value>
        </property>
      </class>
      <else>
      <class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName">
          <value>oracle.jdbc.driver.OracleDriver</value>
        </property>
        <property name="url">
          <value>jdbc&#58;oracle&#58;thin&#58;@xxx&#58;1521&#58;ddd</value>
        </property>
        <property name="username">
          <value>fff</value>
        </property>
        <property name="password">
          <value>rrr</value>
        </property>
        <property name="defaultAutoCommit">
          <value>false</value>
        </property>
      </class>
      </if>
      </bean>

      Comment


      • #4
        I see what you're trying to do, but I typically handle this at development time with the build script, as usually the differences between a dev and production deployment is potentially fairly significant, and I wouldn't put in a bunch of tests like this even if I could.

        Comment


        • #5
          I am trying too eliminate a dependency on Jconfig

          I am trying too eliminate a dependency on Jconfig as I think Spring would do it just as well if it had the functionality http://www.jconfig.org/

          Comment


          • #6
            Any more suggestions on this

            I would like to be able to deploy my application to 2 different systems without having to rebuild an ear. The source is exactly the same and the only thing that's difference is the configuration. I would like to be able to make an ear for all known environments with all known configurations and then be able to deploy the ear without change. Please advise as to the best practice here. Thanks.

            Comment


            • #7
              So if I understand you correctly, you want to set the data source attributes based on system properties. This is easy to do. We do it in our application using a PropertyPlaceholderConfigurer. The documentation sort of implies that you must have an external properties file, but that's not the case. You can rely exclusively on system properties. Here's a snippet from our config file:

              Code:
              <bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
              	<property name="properties">
              		<props>
              			<prop key="jdbc.driverClassName">oracle.jdbc.driver.OracleDriver</prop>
              			<prop key="jdbc.url">jdbc&#58;oracle&#58;thin&#58;@192.168.1.103&#58;1521&#58;orcl9lnx</prop>
              			<prop key="jdbc.username">sa</prop>
              			<prop key="jdbc.password">sa</prop>				
              		</props>
              	</property>
              	<property name="systemPropertiesModeName">
              		<value>SYSTEM_PROPERTIES_MODE_OVERRIDE</value>
              	</property>
              </bean>
              
              ...
              
              <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
                <property name="driverClassName">
              	<value>$&#123;jdbc.driverClassName&#125;</value>
                </property>
                <property name="url">
              	<value>$&#123;jdbc.url&#125;</value>
                </property>
                <property name="username">
              	<value>$&#123;jdbc.username&#125;</value>
                </property>
                <property name="password">
              	<value>$&#123;jdbc.password&#125;</value>
                </property>
              </bean>

              Comment


              • #8
                Oops, sorry. I missed the "if...else" elements in your code. I thought you just wanted to fill in the data source properties.

                Well, we also do what you're trying to do, but not with a single config file. We have a "standalone" config file and a "container" config file. In our class that initializes the ApplicationContext, we have a switch (whose value gets set from a system property) that tells us which config file to load. Then we load one or the other.

                This approach works only as long as you're writing the code that instantiates the ApplicationContext. In our case, we have a service locator class that wraps one. Instantiating that class (via a factory) basically bootstraps everything.

                Comment


                • #9
                  In there a way to do this if you don't write the code

                  i.e via org.springframework.web.context.ContextLoaderServl et

                  Comment

                  Working...
                  X