Spring. Net simple getting started, spring.net getting started

Source: Internet
Author: User

Spring. Net simple getting started, spring.net getting started

Spring. NET IoC container usage.

Use simple examples to learn Spring. Net

1. Create a console program project first.

2. added the IUserInfoDal interface.

namespace Spring.Net{    public interface IUserInfoDal    {        void Show();    }}

3. Add the AdoNetUserInfoDal class and EFUserInfoDal class to inherit the IUserInfoDal interface.

AdoNetUserInfoDal. cs

Public class AdoNetUserInfoDal: IUserInfoDal {public void Show () {Console. WriteLine ("I am AdoNet Dal );}}

EFUserInfoDal. cs

Public class EFUserInfoDal: IUserInfoDal {public void Show () {Console. WriteLine ("I am EF Dal );}}

4. Reference Spring. Net Assembly Spring. Core. dll and Common. Logging. dll

5. Add Spring. Net configuration nodes and configure object nodes.

<? Xml version = "1.0" encoding = "UTF-8"?> <Configuration> <! -- Be sure to add --> <configSections> <! -- Follow Spring below. net node configuration is a one-to-one ing --> <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> <startup> <supportedRuntime version =" v4.0 "sku = ". NETFramework, Version = v4.5 "/> </startup> <! -- Spring. Net node configuration --> <spring> <context> <! -- Container configuration --> <resource uri = "config: // spring/objects"/> </context> <objects xmlns =" http://www.springframework.net "> <! -- All nodes in the container --> <description> An example that demonstrates simple IoC features. </description> <! -- Name must be unique. type = the full name of the class, and the Assembly where the class is located --> <object name = "UserInfoDal" type = "Spring. net. EFUserInfoDal, Spring. net "> </object> </spring> </configuration>

6. Start to write the main function and create the spring container context.

Namespace Spring. net {class Program {static void Main (string [] args) {// control is not reversed // IUserInfoDal infoDal = new EFUserInfoDal (); // Spring. net instance creation method to container help us create // create spring container context IApplicationContext ctx = ContextRegistry. getContext (); // create the IUserInfoDal efDal = ctx through the container. getObject ("UserInfoDal") as IUserInfoDal; efDal. show (); Console. readKey ();}}}

 

7. Property Injection

<! -- Spring. Net node configuration --> <spring> <context> <! -- Container configuration --> <resource uri = "config: // spring/objects"/> </context> <objects xmlns = "http://www.springframework.net"> <! -- All nodes in the container --> <description> An example that demonstrates simple IoC features. </description> <! -- Name must be unique. type = the full name of the class, and the Assembly where the class is located --> <object name = "UserInfoDal" type = "Spring. net. EFUserInfoDal, Spring. net "> <property name =" Name "value =" Zhang San "/> <! -- Ref points to the following property injection --> <property name = "UserInfo" ref = "UserInfo"/> </object> <! -- Property injection --> <object name = "UserInfo" type = "Spring. net. userInfo, Spring. net "> <property name =" Name "value =" "/> <property name =" Age "value =" 15 "/> </object> </objects> </spring>

8. constructor Injection

<! -- Constructor injection --> <object name = "UserInfoDal2" type = "Spring. net. adoNetUserInfoDal, Spring. net "> <constructor-arg index =" 0 "value =" Zhang San "/> <constructor-arg index =" 1 "ref =" UserInfo "/> </object>

9. container configuration

<Context> <! -- Container configuration --> <resource uri = "config: // spring/objects"/> <! -- Xml file method, modify attributes, and copy to the output directory: Always copy --> <! -- <Resource uri = "file: // objects. xml"/> --> <! -- Embedded assembly method: assembly: // assembly name/project name/objects. xml, change attributes, always copy, generate operations, embedded resources --> <! -- <Resource uri = "assembly: // Spring. Net/Spring. Net/objects. xml"/> --> </context>

10. Complete example

IUserInfoDal. cs

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Spring.Net{    public interface IUserInfoDal    {        UserInfo UserInfo { get; set; }        string Name { get; set; }        void Show();    }}

AdoNetUserInfoDal. cs

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace Spring. net {public class AdoNetUserInfoDal: IUserInfoDal {public AdoNetUserInfoDal (string name, UserInfo userInfo) {Name = name; UserInfo = userInfo;} public UserInfo {get; set ;} public string Name {get; set;} public void Show () {Console. writeLine ("I am AdoNet Dal, property injection: Name =" + Name); Console. writeLine ("UserInfo, Name =" + UserInfo. name + "Age =" + UserInfo. age );}}}

EFUserInfoDal. cs

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace Spring. net {public class EFUserInfoDal: IUserInfoDal {public EFUserInfoDal () {} public UserInfo {get; set;} public string Name {get; set;} public void Show () {Console. writeLine ("I'm EF Dal, property injection: Name =" + Name); Console. writeLine ("UserInfo, Name =" + UserInfo. name + "Age =" + UserInfo. age );}}}

App. config

<? Xml version = "1.0" encoding = "UTF-8"?> <Configuration> <! -- Be sure to add --> <configSections> <! -- Follow Spring below. net node configuration is a one-to-one ing --> <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> <startup> <supportedRuntime version =" v4.0 "sku = ". NETFramework, Version = v4.5 "/> </startup> <! -- Spring. Net node configuration --> <spring> <context> <! -- Container configuration --> <resource uri = "config: // spring/objects"/> <! -- Xml file method, modify attributes, and copy to the output directory: Always copy --> <! -- <Resource uri =" file://objects.xml "/> --> <! -- Embedded assembly method: assembly: // assembly name/project name/objects. xml, change attributes, always copy, generate operations, embedded resources --> <! -- <Resource uri = "assembly: // Spring. Net/Spring. Net/objects. xml"/> --> </context> <objects xmlns =" http://www.springframework.net "> <! -- All nodes in the container --> <description> An example that demonstrates simple IoC features. </description> <! -- Name must be unique. type = the full name of the class, and the Assembly where the class is located --> <object name = "UserInfoDal" type = "Spring. net. EFUserInfoDal, Spring. net "> <property name =" Name "value =" Zhang San "/> <! -- Ref points to the following property injection --> <property name = "UserInfo" ref = "UserInfo"/> </object> <! -- Constructor injection --> <object name = "UserInfoDal2" type = "Spring. net. adoNetUserInfoDal, Spring. net "> <constructor-arg index =" 0 "value =" Zhang San "/> <constructor-arg index =" 1 "ref =" UserInfo "/> </object> <! -- Property injection --> <object name = "UserInfo" type = "Spring. net. userInfo, Spring. net "> <property name =" Name "value =" "/> <property name =" Age "value =" 15 "/> </object> </objects> </spring> </configuration>

Program. cs

Using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; // Spring. aop. dll for Aspect-Oriented Programming // Spring. core. dll spring framework basics // Common. logging. dll must also reference namespace Spring. net {class Program {static void Main (string [] args) {// control is not reversed // IUserInfoDal infoDal = new EFUserInfoDal (); // Spring. net instance creation method to container help us create // step 1, reference Spring. net Assembly Spring. core. dll and Common. logging. dll // step 2, add Spring. net configuration node // step 3, configure the object node // Step 4, create the spring container context IApplicationContext ctx = ContextRegistry. getContext (); // Step 5: Create the IUserInfoDal efDal = ctx object through the container. getObject ("UserInfoDal") as IUserInfoDal; efDal. show (); IUserInfoDal adoDal = ctx. getObject ("UserInfoDal2") as IUserInfoDal; adoDal. show (); Console. readKey ();}}}

The following is an example of Spring. Net integration in the MVC project.

1. Copy the Spring.net assembly to the packages folder under the project.

2. Add Spring. Core. dll, Common. Logging. dll, Spring. Web. Mvc4.dll, and Spring. Web. dll to the project.

3. modify Web. config and configure nodes.

<Configuration> <configSections> <! -- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink? LinkID = 237468 --> <section name = "entityFramework" type = "System. data. entity. internal. configFile. entityFrameworkSection, EntityFramework, Version = 5.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934e089 "requirePermission =" false "/> <! -- Spring. net configuration node --> <sectionGroup name = "spring"> <section name = "context" type = "Spring. context. support. mvcContextHandler, Spring. web. mvc4 "/> </sectionGroup> </configSections> <! -- Spring. Net node detailed configuration --> <spring> <context> <resource uri = "file ://~ /Config/controllers. xml "/> </context> </spring> </configuration>

4. Create the Config folder and the controllers. xml file in the project.

<?xml version="1.0" encoding="utf-8" ?><objects xmlns="http://www.springframework.net">  <object type="HX.Shop.UI.Main.Controllers.AdminController, HX.Shop.UI.Main" singleton="false" >    <property name="AdminService" ref="AdminService" />  </object>  <object name="AdminService" type="HX.Shop.BLL.AdminService, HX.Shop.BLL" singleton="false" >      </object></objects>

5. Modify SpringMvcApplication in Global. asax

public class MvcApplication : Spring.Web.Mvc.SpringMvcApplication //System.Web.HttpApplication

 

From: http://www.cnblogs.com/han1982/p/4177850.html

I wrote an example and shared it with 360 free cloud disks.
Download link: https://yunpan.cn/cYWTKQcZUPnxn (extraction code: 880a)

Development Environment: VS2013

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.