第一个简单的Sprint.net例子
第一个简单使用Spring.net的例子
目的是把一个简单的类通过反转注入为一个实例.练习基本的config配置
需要反转的类:
using System;
namespace SpringTest
{
/// <summary>
/// 用户类
/// </summary>
public class User
{
private string name;
private int age;
//属性
public int Age {
get {
return age;
}
set {
age = value;
}
}
public string Name {
get {
return name;
}
set {
name = value;
}
}
//Contructor
public User()
{
}
public User(int age, string name)
{
this.age = age;
this.name = name;
}
}
}
namespace SpringTest
{
/// <summary>
/// 用户类
/// </summary>
public class User
{
private string name;
private int age;
//属性
public int Age {
get {
return age;
}
set {
age = value;
}
}
public string Name {
get {
return name;
}
set {
name = value;
}
}
//Contructor
public User()
{
}
public User(int age, string name)
{
this.age = age;
this.name = name;
}
}
}
App.config里的配置
<?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="config://spring/objects"/>
</context>
<objects>
<object name="MyUser" type="SpringTest.User,SpringTest">
<constructor-arg name="name" type="string"><value>Tommy</value></constructor-arg>
<constructor-arg name="age" type="int"><value>20</value></constructor-arg>
</object>
</objects>
</spring>
</configuration>
<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="config://spring/objects"/>
</context>
<objects>
<object name="MyUser" type="SpringTest.User,SpringTest">
<constructor-arg name="name" type="string"><value>Tommy</value></constructor-arg>
<constructor-arg name="age" type="int"><value>20</value></constructor-arg>
</object>
</objects>
</spring>
</configuration>
具体调用Sprint实现代码:
using System;
using System.Configuration;
using log4net.Config;
using Spring.Context;
namespace SprintTest
{
class MainClass
{
public static void Main(string[] args)
{
BasicConfigurator.Configure();
// IApplicationContext context = ConfigurationSettings.GetConfig("spring/context") as IApplicationContext;
IApplicationContext context =(IApplicationContext)ConfigurationSettings.GetConfig("spring/context");
User user = (User)context.GetObject("MyUser");
Console.WriteLine(user.Name + "-" + user.Age);
}
}
}
using System.Configuration;
using log4net.Config;
using Spring.Context;
namespace SprintTest
{
class MainClass
{
public static void Main(string[] args)
{
BasicConfigurator.Configure();
// IApplicationContext context = ConfigurationSettings.GetConfig("spring/context") as IApplicationContext;
IApplicationContext context =(IApplicationContext)ConfigurationSettings.GetConfig("spring/context");
User user = (User)context.GetObject("MyUser");
Console.WriteLine(user.Name + "-" + user.Age);
}
}
}