Announcement Announcement Module
Collapse
No announcement yet.
How to pass properties values to ClassPathXmlApplicationContext bean? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to pass properties values to ClassPathXmlApplicationContext bean?

    I have to use two beans with same id which defined in two separated jars from third party, so I basically cannot change the name. I decide to use ClassPathXmlApplicationContext to solve the conflict, e.g. in my applicationContext.xml, I define:

    <bean id="firstContext" class="org.springframework.context.support.ClassPa thXmlApplication">
    <constructor-arg><value>classpathackage1.xml</value></constructor-arg>
    </bean>
    <bean id="firstService" class="org.springframwork.beans.factory.config.Bea nReferenceFactoryBean>
    <property name="targetBeanName">
    <value>commonService</value>
    </property>
    <property name="beanFactory"><ref="firstContext"></property>
    </bean>

    The problem is the commonService needs some configuration properties defined in our context, e.g., its constructor definition is like:
    <bean id="commonService>
    <property name="url"><value>http://$serviceHost:$servicePort/
    </property>
    and we include a properties file in our applicationContext.xml like

    <bean class="org.springframework.beans.factory.config.Pr opertyPlaceholderConfigurer">
    <property name="location"> <value>services.properties</value> </property>
    </bean>

    The properties have been successfully loaded (other beans in the applicationsContext.xml have been initialized with the correct value from the services.properties), but the commonService always throw an exception that http://$serviceHost:$servicePort/ is invalid. If I do not use the ClassPathXmlApplicationContext but just import the package1.xml, the commonService bean then can be initialized with the correct values (but I cannot import package2.xml since its commonSevice bean will override the first one). So the question is:

    1. Is there any better way to handle the conflict bean id WITHOUT modifying original definitions?
    2. How to pass the properties values from one application context to an other?

    Thanks in advance!
Working...
X