我试图在以下弹簧:如何在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
当我运行它,结果是这样的:
prop1: value1
prop2: value1
不过,我想为bean1
范围,只需要道具道具文件1!因为它是一个模块和其他Bean2
采取value2
!
这是可以实现的吗?我觉得它非常基本!谢谢!!!
重要提示:我有一个包含子模块的XML对我很重要!通过这种方式,我利用弹簧很好的XML,在那里我定义了哪些bean作为子模块(对我来说也是有用的)。当然,他们也生活在相同的环境中!对我来说非常重要的是,豆类在相同的环境中。
重要:模块是由其他开发人员编写的,我无法控制他们使用哪些属性名称。
重要:父模块必须能够运行子模块bean,这意味着父/子在这里不好,子模块也不需要知道父属性,因为它们只是模块。
但是如果我让别人开发一个模块(比如一个插件),并且他使用了一些偶然在父类中定义的“key”,那么他的模块会收到它吗?我希望这些模块能够分别定义模块创建者彼此之间以及包含者之间不相互通信的属性,因此碰撞可能会发生,我不希望模块从其他模块或父类获取属性。 – Jas 2012-08-01 10:16:43
另外我不能使用父/子上下文,因为我需要我的父上下文来注意子上下文中的bean,因为它使用它们,它将遍历子上下文中的每个bean(如果我们使用子上下文) - 将使用每个模块中的每个bean并依次运行它们。 – Jas 2012-08-01 10:23:47