- 기본사항
개발환경 : Windows&, VisualStudio2010, Spring.Net3.0, Asp.Net4.0
스프링 프레임워크를 이용하여 비지니스로직을 DI/IoC를 이용하여 loose coupling 을 구현합니다.
구현하기 위한 최소한의 설정만으로 설명이 이루어집니다.
- 기본 Web.Config - ASP.NET MVC 2 빈 웹 응용 프로그램으로 생성 합니다.
<?xml version="1.0" encoding="utf-8"?>
<!--
ASP.NET 응용 프로그램을 구성하는 방법에 대한 자세한 내용을 보려면
http://go.microsoft.com/fwlink/?LinkId=152368 을 방문하십시오.
-->
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</assemblies>
</compilation>
<authentication mode="Forms">
<forms loginUrl="~/Account/LogOn" timeout="2880" />
</authentication>
<pages>
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
</namespaces>
</pages>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
- DI/IoC 추가된 Web.config : 하이라이트
<?xml version="1.0" encoding="utf-8"?>
<!--
ASP.NET 응용 프로그램을 구성하는 방법에 대한 자세한 내용을 보려면
http://go.microsoft.com/fwlink/?LinkId=152368 을 방문하십시오.
-->
<configuration>
<!-- add for spring -->
<configSections>
<!-- spring sectionGroup -->
<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>
<!-- add for spring -->
<spring>
<context>
<!-- assembly://Assembly Name/Name Space/파일명 -->
<!-- springBiz.xml위치가 프로젝트 root 경로에 있는 경우 -->
<resource uri="assembly://TestProject.Biz/TestProject.Biz/springBiz.xml" />
<!-- springBiz.xml위치가 프로젝트 root/Config 경로에 있는 경우 -->
<resource uri="assembly://TestProject.Dao/TestProject.Dao.Config/springDao.xml" />
</context>
</spring>
<system.web>
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</assemblies>
</compilation>
<authentication mode="Forms">
<forms loginUrl="~/Account/LogOn" timeout="2880" />
</authentication>
<pages>
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
</namespaces>
</pages>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
- springBiz.xml 속성 > 빌드작업 > 포함 리소스로 변경작업을 합니다.
# 해당 작업을 수행해야 빌드시 xml 파일이 dll에 포함되어 배포됩니다.
- 테스트 소스 작성 및 테스트
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TestProject.Biz;
namespace TestProject.Biz
{
public interface ITestBiz
{
string Hello(string test);
}
public class TestBiz : ITestBiz
{
public string Hello(string test)
{
TestDao testDao = new TestDao();
return "Hello!!!";
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TestProject.Biz;
using TestProject.Controller;
using Spring.Context;
using Spring.Context.Support;
namespace TestProject.Web.Controllers
{
public class TestController
{
public ActionResult Index()
{
//ITestBiz testBiz = new TestBiz();
ITestBiz testBiz = ObjectManager.GetObject("TestBiz") as ITestBiz;
ViewData["Hello"] = testBiz.Hello("HelloTest");
return View();
}
}
public static class ObjectManager
{
public static object GetObject(string strObjectID)
{
IApplicationContext ctx = GetObjectContext();
object obj = ctx[strObjectID];
return obj;
}
public static IApplicationContext GetObjectContext()
{
return ContextRegistry.GetContext();
}
}
} |