Spring.NET은 .NET 기반 기업 애플리케이션 개발을 지원하는 프레임워크로, 의존성 주입(DI), 제어의 역전(IoC), AOP 등 핵심 기능을 제공합니다. 본 가이드는 WinForm 프로젝트를 기반으로 Spring.NET의 핵심 개념을 코드 예제와 함께 설명합니다.
먼저 App.config에 다음 구성을 추가하여 Spring.NET 환경을 준비합니다:
Common.Logging.dll과 Spring.Core.dll을 참조 후, 다음 코드로 컨테이너를 초기화합니다:
다음은 인터페이스와 구현 클래스의 예시입니다:
기존 방식은 직접 인스턴스 생성을 통해 사용했지만, Spring.NET에서는 다음과 같이 컨테이너를 통해 객체를 획득합니다:
생성자 주입을 활용한 예시는 다음과 같습니다:
복합적인 의존성 관리를 위해 UserRepository 클래스를 추가하고 다음과 같이 구성합니다:
이렇게 하면 UI 계층과 비즈니스 로직 간의 강한 결합을 해소할 수 있습니다. Spring.NET의 IoC 컨테이너를 통해 객체 생성과 관계 설정을 외부에서 처리함으로써 유연한 시스템 구조를 구축할 수 있습니다.
<configSections>
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
</sectionGroup>
</configSections>
<spring>
<context>
<resource uri="config://spring/objects"/>
</context>
<objects xmlns="http://www.springframework.net">
<description>기본 IoC 기능 예제</description>
</objects>
</spring>
IApplicationContext ctx = ContextRegistry.GetContext();
public interface IUserService {
void Display();
}
public class UserService : IUserService {
public void Display() {
Console.WriteLine("Spring.NET Demo 실행");
}
}
<objects>
<object name="UserService" type="SpringNetDemo.UserService, SpringNetDemo">
<property name="SystemName" value="개발자"/>
</object>
</objects>
IUserService service = (IUserService)ctx.GetObject("UserService");
service.Display();
// App.config
<constructor-arg name="userAge" value="25"/>
// C# 코드
public UserService(int userAge) {
Age = userAge;
}
public class UserRepository {
public string Username { get; set; }
}
<objects>
<object name="UserRepo" type="SpringNetDemo.UserRepository, SpringNetDemo">
<property name="Username" value="admin"/>
</object>
<object name="UserService" type="SpringNetDemo.UserService, SpringNetDemo">
<property name="UserRepo" ref="UserRepo"/>
</object>
</objects>