Announcement Announcement Module
Collapse
No announcement yet.
How to contribute to a service from multiple configurations. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to contribute to a service from multiple configurations.

    Hi

    I have several application modules with their own spring configuration.
    The common or the war module imports all the spring configurations from individual app modules into its main spring configuration.

    I have a service/bean that takes a list of properties, and has some common functionality.

    What I want to do is to supply some properties from each individual app modules to this common service/bean.

    In hivemind I could create a configuration and each individual module could contribute to this configuration and the common service can read this configuration to get all the contributions to it.

    How can I do this in Spring?

    Thanks
    Srikanth

  • #2
    You mean something like this?

    spring-main.xml

    Code:
        
    <import resource="classpath:spring-support.xml"/>
    
    <bean id="service" class="com.service.impl.MyServiceImpl">
            <property name="mySupport" ref="mySupport"/>
    </bean>
    spring-support.xml

    Code:
        
    <bean id="mySupport" class="com.support.MySupportImpl"/>

    Comment


    • #3
      You were close...

      The spring-main.xml has the bean definition and the spring-support.xml will add that property to the bean.

      There could be more than one supporting xml files, like spring-support1.xml, spring-support2.xml.

      I want get all the properties from all supporting xml configurations in to that bean.


      [I am excited that somebody responded ]

      Thanks

      Comment


      • #4
        i think i responded too quickly....

        spring-main.xml

        Code:
        <import resource="classpath:spring-support.xml"/>
        
        <bean id="service" class="com.service.impl.MyServiceImpl">
                <property name="mappings">
        		<props>
        			<prop key="Property1">Value1</prop>			
        		</props>
        	</property>
        </bean>
        spring-support.xml

        Code:
        <bean id="service" class="com.service.impl.MyServiceImpl">
             <property name="mappings">
        	 <props>
        		<prop key="Property2">Value2</prop>
        	</props>
            </property>
        <bean>
        I know what I did here is not right, but what i want at the end is, when spring-main.xml is loaded and the bean "service" is created, i want to see two properties in that property "mappings".

        Thanks
        Last edited by smadarapu; Mar 9th, 2011, 12:24 PM. Reason: fixed a typo.

        Comment


        • #5
          Originally posted by smadarapu View Post
          You were close...

          The spring-main.xml has the bean definition and the spring-support.xml will add that property to the bean.

          There could be more than one supporting xml files, like spring-support1.xml, spring-support2.xml.

          I want get all the properties from all supporting xml configurations in to that bean.


          [I am excited that somebody responded ]

          Thanks
          Ya . You can import all the support xmls to your main xml,

          Code:
          <import resource="classpath:spring-support1.xml"/>
          <import resource="classpath:spring-support2.xml"/>
          Last edited by amiladomingo; Mar 9th, 2011, 12:30 PM.

          Comment


          • #6
            I think i also replied too quickly

            I didn't get your requirement. Why are you creating two beans with same name?

            Comment


            • #7
              I am not creating two beans, i want to add properties to the bean from different configuration files like support-1, support-2... support-n. I am trying to find how to do it.

              The code/configuration i posted is just conceptual, I want to create only one bean with the properties consolidated from all the supporting configuration files.

              Thanks

              Comment


              • #8
                Its disappointing that I don't get any answers here.

                So I decided to create wrapper bean that wraps an instance of Properties (static member)


                Code:
                public class EndpointConfigurationConsolidator {
                
                    private static Properties mappings = new Properties();
                
                    /**
                     * Returns the mappings.
                     * 
                     * @return
                     */
                    public Properties getMappings() {
                        return mappings;
                    }
                
                    /**
                     * Caches all the properties from all sub-modules.
                     * 
                     * @see PayloadRootQNameEndpointMapping#setMappings(Properties).
                     */
                    public void setMappings(Properties mappings) {
                        EndpointConfigurationConsolidator.mappings.putAll(mappings);
                    }
                }
                Then in each sub module I am doing the following

                Code:
                	<!-- This bean is defined as a way to contribute the properties to the "endpointMappings" 
                		bean. This bean "emMappings" is not used otherwise. The setMappings will 
                		do what is needed. -->
                	<bean id="emMappings"
                		class="com.workscape.soa.configurations.EndpointConfigurationConsolidator" >
                		<property name="mappings">
                			<props>
                				<prop key="{http://www.hr-xml.org/3}SyncCandidate">syncSpringWSCandidateEndpoint</prop>
                				<prop key="{http://www.hr-xml.org/3}GetCandidate">getSpringWSCandidateEndpoint</prop>
                			</props>
                		</property>
                	</bean>
                Finally, in my main context xml doing the following...

                Code:
                	<!-- This bean is here to get access to the mappings contributed by all the sub-modules. The "endpointMappings" bean will use this bean to get the consolidated list of properties. -->
                	<bean id="allMappings"
                		class="com.workscape.soa.configurations.EndpointConfigurationConsolidator" >
                	</bean>
                
                	<bean id="consolidatedMappings" factory-bean="allMappings" factory-method="getMappings"/>
                
                	<bean id="endpointMappings"
                		class="org.springframework.ws.server.endpoint.mapping.PayloadRootQNameEndpointMapping">
                		<property name="mappings">
                			<ref bean="consolidatedMappings" />
                		</property>
                
                		<property name="interceptors">
                			<list>
                				<bean class="org.springframework.ws.server.endpoint.interceptor.PayloadLoggingInterceptor" />
                			</list>
                		</property>
                	</bean>
                I don't like this solution, but this is the only way I could find so far and it works. If anybody has a better solution, let me know.

                Comment


                • #9
                  Is it something like this you looking for?

                  You can have PropertyClass's defined in all the support xmls.

                  Code:
                  public class ProperyClass {
                  
                  	private Properties properties;
                  
                  	public void setProperties(Properties properties) {
                  		this.properties = properties;
                  	}
                  
                  	public Properties getProperties() {
                  		return properties;
                  	}
                  }
                  PropertyLoader can be defined in main xml. It will get all the properties of ProperyClass classes and load them to allProps property.

                  Code:
                  public class PropertyLoader implements InitializingBean,
                  		ApplicationContextAware {
                  
                  	private ApplicationContext appContext;
                  
                  	private Properties allProps = new Properties();
                  
                  	@Override
                  	public void afterPropertiesSet() throws Exception {
                  		Map<String, ProperyClass> beanMap = appContext
                  				.getBeansOfType(ProperyClass.class);
                  
                  		for (final ProperyClass props: beanMap.values()) {
                  			allProps.putAll(props.getProperties());
                  		}
                  	}
                  
                  	@Override
                  	public void setApplicationContext(ApplicationContext arg0)
                  			throws BeansException {
                  		appContext = arg0;
                  	}
                  }
                  Last edited by amiladomingo; Mar 10th, 2011, 12:57 PM. Reason: changed var name

                  Comment


                  • #10
                    I was hoping not to do any coding, i have a feeling that there is some feature available in Spring (similar to hivemind) but i just don't know what and how to use that feature.

                    i have already wrote some code (as you might have seen above) that is doing what i wanted to do. I just don't like the fact that i have to do some coding for this basic requirement, seems like several people are looking for the same.

                    Thanks.

                    Comment

                    Working...
                    X