learn spring from today. net, this series of articles follow a newbie (such as me) the confusions encountered in the practical process and the solutions to the confusions are written. As a learning note for learning spring. net.
this series of articles assumes that you have a preliminary understanding of the basic theoretical knowledge of spring. net, such as IOC and AOP. The focus is on the process of practical learning after mastering the relevant theoretical knowledge.
Chapter 1: Getting started; how to build your first spring.. Net instance
created in total console " Hello World " instance. Specific process:
( 1 ) add spring. core reference
(2) WriteHello. CSIs a simple attribute, helloworld.
Hello. CS
Namespace springstudy_1
{
Class hello
{
Private String helloword;
Public String helloword
{
Get {Return This. Helloword ;}
Set {This. Helloword=Value ;}
}
}
}
(3) AddXMLConfiguration file. The configuration file content is as follows:
< 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 ID = "Hello" Type = "Springstudy_1.hello" >
< Property Name = "Helloword" Value = "Hello! Welcome to spring. Net word! " />
</ Object >
</ Objects >
(4) Write callProgram. The caller must reference
UsingSpring. Core. IO;
UsingSpring. Objects. factory;
UsingSpring. Objects. Factory. xml;
The call method is as follows:
Static Void Main (string [] ARGs)
{
// Iresource rs = new filesystemresource ("objects-config.xml ");
Iresource rs = New Filesystemresource ( " File: // objects-config.xml ");
Iobjectfactory Factory = New xmlobjectfactory (RS );
Hello hello = (Hello) Factory. GetObject ( " Hello ");
System. Console. Out. writeline (hello. helloword );
System. Console. In. Read ();
}
Note: If the following call statement is used
Iresource rs = new filesystemresource ("objects-config.xml ");
The system willFind the file under bin/debug. Therefore, you need to set thisThe attributes of the XML file are:
Copy to output directory: if new, copy
You can also use the following method to call iapplicationcontext..IapplicationcontextExtended implementationAdvanced functions not implemented by iobjectfactory.
Iapplicationcontext Context = New Xmlapplicationcontext ( " Objects-config.xml ");
// Of course, an iapplicationcontext is also an iobjectfactory
Iobjectfactory Factory = (Iobjectfactory) context;
Hello hello = (Hello) Factory. GetObject ( " Hello ");
The other is to eliminateHard encoding of the objects-config.xml. Add the configuration file app. config as follows:
<? 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 >
< Spring >
< Context >
< Resource Uri = "File: // objects-config.xml" />
</ Context >
< Objects Xmlns = "Http://www.springframework.net" >
< Description > An example that demonstrates simple IOC features. </ Description >
</ Objects >
</ Spring >
</ Configuration >
In the configuration file, specifyResource path. Then you can call the following method in the program:
IapplicationcontextCTX =Contextregistry. Getcontext ();
HelloHello = (Hello) CTX. GetObject ("Hello");
Note: InIn vs2005, when there is no reference to the class, you can useCTRL + Shift + F10 to add references to related resources. (Similar eclipse methods)
So far, the first spring. Net instance has been completed. The Hello class is injected in main.