I tried following in spring:
modules.xml:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<import resource="classpath:module1.xml"></import>
<import resource="classpath:module2.xml"></import>
</beans>
module1.xml:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:prop1.properties"></property>
</bean>
<bean id="bean1" class="Bean1">
<property name="prop1" value="${key}"/>
</bean>
</beans>
module2.xml:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:prop2.properties"></property>
</bean>
<bean id="bean2" class="Bean2">
<property name="prop2" value="${key}"/>
</bean>
</beans>
Bean1.java:
import org.springframework.beans.factory.annotation.Autowired;
public class Bean1 {
@Autowired
public String prop1;
public void setProp1(String val) {
prop1 = val;
}
}
bean2.java:
import org.springframework.beans.factory.annotation.Autowired;
public class Bean2 {
@Autowired
public String prop2;
public void setProp2(String val) {
prop2 = val;
}
}
ModulesBean.java:
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ModulesBean {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
new String[] {"modules.xml"});
BeanFactory beanFactory = (BeanFactory) appContext;
Bean1 bean1 = (Bean1) beanFactory.getBean("bean1");
System.out.println("prop1: " + bean1.prop1);
Bean2 bean2 = (Bean2) beanFactory.getBean("bean2");
System.out.println("prop2: " + bean2.prop2);
}
}
prop1.properties:
key=value1
prop2.properties:
key=value2
When I run it, the result is this:
prop1: value1
prop2: value1
However I want to have scope for bean1 to take only props from props file 1!!! As it’s a module and other Bean2 to take value2!!!
Is that achievable? I find it so very basic! Thanks!!!
Important: it is important to me that I have an XML which includes the submodules! In this way I utilize springs nicely XML which there I define which beans I have as submodules (useful for other things for me as well). Also they live of course in same context! Very important for me that beans are in same context.
important: modules are written by other developers, I have no way to control which property names they use.
important: parent module must be able to run the child modules beans which means parent/child is not good here, also child modules need not be aware of parent properties because they are just modules.
Consider using Parent/Children Spring context hierarchy. For example, you can create a parent context and two children contexts, load your property files in an appropriate one. Beans which were loaded in the parent one are shared between all children contexts, but children contexts has their own scope for beans.
You can find more information on the Internet.