이것은 구현하려는 상황입니다.자식 컨텍스트 개체 생성에 대한 부모 컨텍스트 개체에 알립니다.
내가ComponentRepository
모든
IComponent
객체를 등록하려고합니다
public class ComponentRepository
{
public void Register(IComponent component)
{
// store component in collection
}
}
<!-- root context -->
<object id="Repository" type="NameSpace.ComponentRepository" />
<!-- child context (0 - n contexts) -->
<object id="Component" type="NameSpace.Component"/>
. 일종의 게시/가입 메커니즘을 사용하여이를 수행 할 수 있다고 가정하지만 클래스를 깨끗하게 유지하고 싶습니다 (spring.net 인터페이스를 구현하지 않고). 이것을 달성하는 가장 좋은 방법은 무엇입니까?
'ComponentRepository'를 만들기 위해 [method injection] (http://www.springframework.net/doc-latest/reference/html/objects.html#objects-method-injection)을 사용할 수 있습니다.'IApplicationContextAware' , 예를 들어 여기 내 대답을 참조하십시오 : http://stackoverflow.com/questions/5026711/possible-getobjectsoftype-replacement/5195658#5195658 – Marijn
정말로 좋은 방법은 replacer를 사용하는 것입니다. – tobsen
나는 tobsen의 솔루션을 거의 사용하지 않았습니다. 의존성으로'ComponentRepository' 대신'Component'를 취하는 새로운'IApplicationContextAware' 클래스를 만들었습니다. 그런 다음'IConfigurableApplicationContext.ParentContext'를 사용하여'ComponentRepository'를 가져오고'Component'를 등록했습니다. 당신의 도움을 주셔서 감사합니다. – Filip