ServiceLocator 기능을 찾고 있다고 생각합니다. 설정 변경만으로 spring.net으로 달성 할 수 있다고 생각하지 않습니다. ServiceLocator
패턴은 일반적으로 객체가 자신의 di 컨테이너를 인식하기 때문에 종속성 주입 시점에서 권장하지 않습니다.
ServiceLocator
이 필요하고 객체를 스프링 DI 컨테이너에 묶어도 상관없는 경우 은 일 수 있습니다. 당신이 Dictionary<CommandType, string>
와 현재 Dictionary<CommandType, Command>
를 대체하여 설정을 봄에
public class CommandManager
{
Dictionary<CommandType, Command> { get; set; } // set using DI
public Command GetBy(CommandType cmdKey)
{
return Dictionary[cmdKey];
}
}
개체의 이름을 열거 값을지도 :
나는 현재 코드는 다음과 같은 것입니다 가정합니다. 그런 다음 원하는 객체를 얻기 위해 현재의 스프링 컨텍스트를 사용
using Spring.Context;
using Spring.Context.Support;
public class CommandManager
{
Dictionary<CommandType, string> { get; set; } // set using DI; values are object names
public Command GetBy(CommandType cmdKey)
{
string objName = Dictionary[cmdKey];
IApplicationContext ctx = ContextRegistry.GetContext();
return (Command)ctx.GetObject(objName);
}
}
prototype
에 명령 개체의 범위를 설정하는 것을 잊지 마십시오,
<object name="moveCommand"
type="Example.Command.MoveCommand, CommandLib"
scope="prototype">
<property name="StepSize" value="10" />
</object>
이제 때마다 CommandManager.GetBy(myKey)
가 호출되는 새를 인스턴스가 생성됩니다.
"명령 유형 값으로 인스턴스 생성"이란 무엇을 의미합니까? 당신의 코드에서 의미합니까? – Marijn
지금은 Dictionary를 초기화하는 스프링에서이 매핑을 만듭니다. 물론,이 경우에는 singleton Command 객체가 있습니다. –
alexey