나는 봄에 다음과 같은 시도 :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
그러나 나는 소품 1 파일에서만 소품을하는 bean1
에 대한 범위를 갖고 싶어! 모듈 이니까 다른 Bean2
을 가지고 value2
!!!
달성 가능합니까? 나는 아주 기초적인 것을 발견한다! 감사!!!
중요 : 하위 모듈을 포함하는 XML을 가지고 있다는 것이 중요합니다. 이런 방식으로 나는 스프링을 멋지게 XML을 사용하는데 거기서 어떤 콩을 하위 모듈로 정의 했는가? (다른 것들에도 유용하다.) 또한 그들은 같은 맥락에서 물론 살고 있습니다! 콩이 같은 맥락에서 나에게 매우 중요합니다.
중요 : 다른 개발자가 모듈을 작성 했으므로 어떤 속성 이름을 사용할지 제어 할 방법이 없습니다.
중요 : 상위 모듈은 하위 모듈 bean을 실행할 수 있어야합니다. 즉 부모/하위 모듈이 좋지 않음을 의미합니다. 또한 하위 모듈은 단지 모듈이므로 상위 특성을 인식하지 않아도됩니다.
하지만 누군가가 (플러그인과 같은) 모듈을 개발하게하고 우연히 부모가 정의한 우연히 그의 모듈이 올바르게 수신하게하는 "키"를 사용한다면? 모듈이 각각의 속성을 정의 할 수 있기를 원한다면 모듈 생성자는 서로 통신하지 않고 includer와 통신 할 수 없으므로 충돌이 발생할 수 있고 모듈이 다른 모듈이나 부모로부터 속성을 얻는 것을 원하지 않습니다. – Jas
또한 부모 컨텍스트를 사용할 수 없습니다. 왜냐하면 부모 컨텍스트를 사용하여 자식 컨텍스트의 빈을 인식해야하기 때문에 부모 컨텍스트를 사용할 수 없습니다. 자식 컨텍스트에서 각 빈을 트래버스하는 것입니다. 모든 모듈의 모든 빈을 사용하고 하나씩 차례대로 실행하십시오. – Jas