[ASP.NET] Spring.Net Quick Start

Posted by peterj on Sun, 18 Aug 2019 12:40:30 +0200

Spring.NET-2.0.0-M2 and Chinese Documents

Download:

Links: https://pan.baidu.com/s/1TnMWP6wESllwlDE1Gdibqw 
Extraction code: y0r5

 

Spring framework is an open source framework with many applications on Java platform. Although the language is fixed, the good method should be universal, so the Spring framework was moved from Java platform to. NET platform by programmers.

With Spring. NET, we can configure applications in a unified and transparent way. Spring. NET focuses on providing declarative transaction management for the middle tier and a fully functional ASP.NET extension framework. Spring.NET is non-intrusive, and the code does not depend on the framework itself.

Spring.NET can provide many functions, such as control inversion (IoC), dependency injection (DI), aspect-oriented programming (AOP), data access abstraction, and ASP.NET integration.

Spring.NET Core:

The Spring.Core library is the foundation of the framework, providing dependency injection capabilities. Most class libraries in Spring NET depend on or extend Spring.Core's functionality. The IObjectFactory interface provides a simple and elegant factory model that removes the need to locate stub s for singletons and some services. Allow you to decouple real program logic from configuration. As an extension to IObjectFactory, the IApplicationContext interface is also in the Spring.Core library.
 -------- 

Quoted from: https://blog.csdn.net/ruisenLi/article/details/83382267

 

Ioc

Inversion of Control Control Control Inversion

 

DI

Dependency Injection Dependency Injection

 

 

Spring.Net Container

1. Initialization of container objects

2. Load the xml file and put it in memory

3. Create objects according to the xml configuration in the container

 

Introducing dll

Common.Logging.dll and Spring.Core.dll

 

New project, new Test class, for testing

public class Test
{
    public void Show()
    {
        Console.WriteLine("Ha-ha");
    }
}

 

Add configuration in App.config

<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>An  example that demonstrates simple IoC features.</description>
    <object name="Test" type="SpringNetTest.Test, SpringNetTest"></object>
  </objects>
</spring>

Where object SpringNetTest.Test represents the full name of the class, SpringNetTest is the assembly where the class resides.

Normally, Spring.Net is not used.

Test test = new Test();
test.Show();

Using Spring.Net

//Initialization container
IApplicationContext ctx = ContextRegistry.GetContext();
//Request-defined objects
Test test2 = (Test)ctx.GetObject("Test");
test2.Show();

 

When there are many classes, each class is configured in App.config

We can also refer to xml files

New xml file

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.net
		http://www.springframework.net/xsd/spring-objects.xsd">
  <object name="Test1" type="SpringNetTest.Test, SpringNetTest"></object>
</objects>

Set the xml file to always copy

Configuration in App.config

<context>
  <resource uri="config://spring/objects"/>
  <resource uri="file://config.xml"/>
</context>

Establish

Test test3 = (Test)ctx.GetObject("Test1");
test3.Show();

 

We can also read xml files in assemblies

xml files need to be set as embedded resources

In App.config

<context>
  <resource uri="config://spring/objects"/>
  <resource uri="file://config.xml"/>
  <resource uri="assembly://SpringNetTest/SpringNetTest/config.xml"/>
</context>

Establish

Test test4 = (Test)ctx.GetObject("Test1");
test4.Show();

 

 

Topics: Spring xml Java Programming