2010. 4. 19. 11:55
DI를 하기위한 spring.net에서 제공하는 object 기본 생성 방식은 아래같다.
아래와 같은 방법외에 더 있다면 있는데로 추가하도록 하겠다.

▲ dll 참조추가
 - Spring.Core

▲ configSection 설정
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <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>
   ...
</configuration>


▲ object 설정

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections ...>

  <!-- 1. 같은 config 내에서 object 설정-->
  <spring>
    <context>
      <resource uri="config://spring/objects" />
    </context>
    <objects xmlns="http://www.springframework.net">
      <object name="name" type="namespace.class_name, assembly_name" singleton="true/false" />
      ...
    </objects>

  <!-- 2. 포함리소스 방식 -->
  <spring>
    <context>
      <resource uri="assembly://Spring.IocQuickStart.MovieFinder/
                         Spring.IocQuickStart.MovieFinder/AppContext.xml"/>
    </context>
  </spring>

  <!-- 3. object 별로 xml 분리하여 설정 -->
  <spring>
    <context>
      <resource uri="~/Config/CommonObjects.xml"/>

      <resource uri="~/Config/Production/Services.xml"/>

      <resource uri="file://%PROGRAMFILES%/myapp/ioc.config" />
    </context>
  </spring>

</configuration>

2번으로 할 경우는 필히 잊지말고 관련파일을 해당프로젝트(assembly)에 포함리소스로 변경해주어야 한다.



1,2,3을 혼용해서 사용이 가능한지 모르겠지만 개인적으로 3번처럼 구성하는게 좋아보인다.


▲ config에서 객체생성
  - 기본객체 생성
  - 프로퍼티 형식으로 객체 주입하여 생성
  - constructor의 생성자에 인자로 객체생성 방법

c#
using Spring.Context;
using Spring.Context.Support;
...
IApplicationContext ctx = ContextRegistry.GetContext();
IHello hello = (IHello)ctx.GetObject("MyHello");  // config의 object의 name과 일치
string result = hello.Say("CoolDragon");
Console.WriteLine(result);


Posted by CoolDragon