Giter Site home page Giter Site logo

springnetsampleapp's Introduction

使用 Spring 框架進行 Dependency Injection 的範例

可在 app.config 直接指定實作物件名稱,或從指定格式的 xml 文件中進行注入設定

環境

  • .NET Framework 4.5.1
  • Spring.Core 2.0.1

情境

在 App.config 選擇要將注入設定

<!-- 使用指定 xml 檔注入 -->
<!--
<spring>
	<context>
		<resource uri="file://service.xml"/>
	</context>
</spring>
-->

<!-- 直接在 app.config 做注入 -->
<spring>
	<context>
		<resource uri="config://spring/objects"/>
	</context>
	<objects xmlns="http://www.springframework.net">
		<object name="InjectDateTime" type="SpringNetSampleApp.BirthDateTime, SpringNetSampleApp">
		</object>
	</objects>
</spring>

透過抽離 DateTime 介面依 App.config 進行不同的注入實作

<spring>
	<context>
		<resource uri="config://spring/objects"/>
	</context>
	<objects xmlns="http://www.springframework.net">
		<!-- 取消註解來指定 IDateTime 介面的實作物件 -->
		<!-- 
		<object name="InjectDateTime" type="SpringNetSampleApp.BirthDateTime, SpringNetSampleApp"></object>
		<object name="InjectDateTime" type="SpringNetSampleApp.UtcDateTime, SpringNetSampleApp"></object>
		<object name="InjectDateTime" type="SpringNetSampleApp.LocalDateTime, SpringNetSampleApp"></object>
		 -->
	</objects>
</spring>

執行後就可以看到主控台輸出結果

參考文件

https://springframework.net/

springnetsampleapp's People

Contributors

txstudio avatar

Stargazers

Horace avatar

Watchers

James Cloos avatar  avatar

Forkers

bubdm

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.